English 中文(简体)
Access system:string resource dictionary XAML in C#
原标题:

How do I access the contents in a resource dictionary using C#?

For example, here is my code in XAML:

<system:String x:Key="NewGroup">New Group Name</system:String>

And I want to access it here in C#:

    private void OnAddGroup(object sender, ExecutedRoutedEventArgs e)
    {

        BooksGroupInfo group = new BooksGroupInfo();
        group.GroupName = "New Group" + TheTabControl.Items.Count;
        TabItem tab = AddGroup(group);
        _currentLibrary.addGroup(group);
        _currentLibrary.CurrentGroup = group;

    }

Instead of typing "New Group" in C#, I would like to replace that and have access in the resource dictionary in XAML. So the command will automatically get the Name that is in the resource dictionary.

I ve tried a couple of solutions like:

(System.String)this.FindResource("NewGroup"); 
Application.Current.Resources[typeof(System.String)]; 

and so on... but they do not seem to work.

I am doing a Localization using locbaml and it doesn t parse the Text/Name on C# (or I don t know how to) and that was the only solution I thought was possible.

问题回答

Usually using FrameworkElement.FindResource like this: string s = this.FindResource("NewGroup") as string; works. It is more likely that the resource with the key "NewGroup" does not exist in the scope of your control or window (whatever this is). You must make sure that the resource is there. E.g. if your resource comes from another file you have to use MergedDictionaries. You can test if the resource is actually acessible try to acess it from XAML that belongs to your codebehind where OnAddGroup is definde.

I hope that makes sense.

If you cannot guarantee the existence of your resources then using

FrameworkElement.TryFindResource

will be a better solution. It is similar to FindResource but rather than throwing an exception, TryFindResource returns null if no resource with the provided key is found. (Implementation of TryXxx() methods is called TryGet Pattern.) Sample in XAML code-behind:

button.Content = this.TryFindResource("NewGroup");




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签