John Ureke
John Ureke

Reputation: 177

MAUI Unable to use string resource file (resx) in XAML

I'm using Visual Studio 17.4.4 on Windows.

Everything I've read says this should be working, but I get: XamlC error XFC0000: Cannot resolve type "MyApp.Resources.Localization:AppStrings" What am I missing?

Here's the xaml:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="MyApp.Resources.Localization"
             xmlns:views="clr-namespace:Jetty_Mobile.Views"
             x:Name="thisPage"
             x:Class="MyApp.MainPage">
    <VerticalStackLayout
        Spacing="25"
        Padding="30,0"
        VerticalOptions="Center">
        <Label Text="{x:Static local:AppStrings.AppName}" />
    </VerticalStackLayout>
</ContentPage>

Looking for AppStrings in code behind works:

public MainPage()
{
    InitializeComponent();
    var thisworks = MyApp.Resources.Localization.AppStrings.AppName;
}

Here is a pic of the resx file

And here is AppStrings.Designer.cs

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MyApp.Resources.Localization {
    using System;
    
    
    /// <summary>
    ///   A strongly-typed resource class, for looking up localized strings, etc.
    /// </summary>
    // This class was auto-generated by the StronglyTypedResourceBuilder
    // class via a tool like ResGen or Visual Studio.
    // To add or remove a member, edit your .ResX file then rerun ResGen
    // with the /str option, or rebuild your VS project.
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    public class AppStrings {
        
        private static global::System.Resources.ResourceManager resourceMan;
        
        private static global::System.Globalization.CultureInfo resourceCulture;
        
        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal AppStrings() {
        }
        
        /// <summary>
        ///   Returns the cached ResourceManager instance used by this class.
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        public static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Jetty_Mobile.Resources.Localization.AppStrings", typeof(AppStrings).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }
        
        /// <summary>
        ///   Overrides the current thread's CurrentUICulture property for all
        ///   resource lookups using this strongly typed resource class.
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        public static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }
        
        /// <summary>
        ///   Looks up a localized string similar to My App.
        /// </summary>
        public static string AppName {
            get {
                return ResourceManager.GetString("AppName", resourceCulture);
            }
        }
    }
}

I have searched all over the web and everything I see says this should work, but it doesn't.

Upvotes: 2

Views: 3057

Answers (2)

Liyun Zhang - MSFT
Liyun Zhang - MSFT

Reputation: 14434

It should be xmlns:local="clr-namespace:MyApp.Resources.Localization"

I have created a sample to test your code:

In the xaml:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local ="clr-namespace:JsonData.Resources.Localization"
             x:Class="JsonData.MainPage">
 <Label Text="{x:Static local:AppStrings.AppName}"/>

The resource file:

enter image description here

And then, the label will show the right value. And when I deleted the clr-namespace in the xmlns:local. I met the same error as yours.

Upvotes: 3

ToolmakerSteve
ToolmakerSteve

Reputation: 21321

"{x:Static local:AppStrings.AppName}"

The error message suggests that local:AppStrings is the wrong syntax to use inside of a property string. AFAIK, those xmlns are for xml element names; e.g. immediately after <.

Try:
"{x:Static MyApp.Resources.Localization.AppStrings.AppName}"

(I'm not sure what syntax is, to shorten it.)

Upvotes: 0

Related Questions