VinceL
VinceL

Reputation: 339

Why does a String in the Resources of a Xamarin Forms application give a Missing default constructor error?

I am trying to declare a string in the Application.Resources section. I have seen examples of this on the web, but only when the System assembly refers to mscorlib.

So if I create a WPF .Net Framework app and then in the App.xaml file have the below, this will compile successfully:

<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>

But if I create a Xamarin Forms mobile app and in the App.xaml file have the below:

<?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>

This then causes a compile error of "Missing default constructor for 'System.String'"

Is there any way of having a string in the resources section without getting this error?

Upvotes: 0

Views: 337

Answers (1)

user10627299
user10627299

Reputation: 9234

If you want to put the string to the Application.Resources in the Xamarin.Forms. You can add String tab like following code format.

<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>

Then used in other layout by StaticResource

 <StackLayout>
        <!-- Place new controls here -->
        <Label Text="{StaticResource mystr}" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
    </StackLayout>

Here is running screenshot.

enter image description here

Upvotes: 1

Related Questions