C# 为什么Xamarin表单应用程序的资源中的字符串会给出缺少的默认构造函数错误?
我试图在Application.Resources部分声明一个字符串。我在web上看到过这样的示例,但仅当系统程序集引用mscorlib时 因此,如果我创建了一个WPF.Net Framework应用程序,然后在app.xaml文件中包含以下内容,这将成功编译:C# 为什么Xamarin表单应用程序的资源中的字符串会给出缺少的默认构造函数错误?,c#,xamarin.forms,C#,Xamarin.forms,我试图在Application.Resources部分声明一个字符串。我在web上看到过这样的示例,但仅当系统程序集引用mscorlib时 因此,如果我创建了一个WPF.Net Framework应用程序,然后在app.xaml文件中包含以下内容,这将成功编译: <Application x:Class="WpfAppNETFrameworkTestString.App" xmlns="http://schemas.microsoft.com/winfx/2006
<Application x:Class="WpfAppNETFrameworkTestString.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfAppNETFrameworkTestString"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
StartupUri="MainWindow.xaml">
<Application.Resources>
<sys:String x:Key="myString">my string</sys:String>
</Application.Resources>
</Application>
我的绳子
但是,如果我创建了一个Xamarin Forms mobile应用程序,并且在app.xaml文件中包含以下内容:
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=netstandard"
mc:Ignorable="d"
x:Class="XamarinFormsTestString.App">
<Application.Resources>
<sys:String x:Key="myString">my string</sys:String>
</Application.Resources>
</Application>
我的绳子
这将导致编译错误“缺少'System.String'的默认构造函数”
有没有办法在参考资料部分使用字符串而不出现此错误?如果要将字符串放入
Xamarin.Forms
中的应用程序.resources
。您可以像下面的代码格式一样添加String
选项卡
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="App24.App">
<Application.Resources>
<x:String x:Key="mystr">ssgsdgsdg</x:String>
</Application.Resources>
</Application>
下面是跑步截图
谢谢,这很有效。有趣的是,Xamarin.Forms中的x名称空间具有这些类型,而WPF中则不是这样。我从来没有想过要尝试这样做——在我搜索这个主题时,我总是找到对系统程序集的引用,因此如果有关于使用x名称空间的文档,我一定错过了它。再次感谢您的快速帮助。
<StackLayout>
<!-- Place new controls here -->
<Label Text="{StaticResource mystr}"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>