如何在UWP xaml中添加变量?

hgtggwj0  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(155)

我想给我的xaml添加一个变量。
我的xaml看起来像这样

<Page
x:Class="UwpTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UwpTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="using:System"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Page.Resources>
    <system:String x:Key="var1">1.2</system:String>
</Page.Resources>
<Grid>

</Grid>
</Page>

运行时,我得到运行时错误。

Microsoft.UI.Xaml.Markup.ReflectionHelperException
  HResult=0x80131500
  Message=Error in reflection helper.  Please add '<PropertyGroup><EnableTypeInfoReflection>false</EnableTypeInfoReflection></PropertyGroup>' to your project file..  Created Xaml type 'String' has a different name than requested type 'System.String'
  Source=Windows
  StackTrace:
   at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)
   at UwpTest.MainPage.InitializeComponent() in D:\Dev\UwpTest\UwpTest\obj\x86\Debug\MainPage.g.i.cs:line 33
   at UwpTest.MainPage..ctor() in D:\Dev\UwpTest\UwpTest\MainPage.xaml.cs:line 27
w7t8yxp5

w7t8yxp51#

我想在我的xaml中添加一个变量
例如,您可以将x:String作为变量添加到资源中

<x:String x:Key="var1">Hello</x:String>

有关更多信息,请参阅XAML intrinsic data types

相关问题