在共享类中如何使用本地资源? [英] In shared classes how to use local resources?

查看:175
本文介绍了在共享类中如何使用本地资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父项目和一个子项目。在子项目中,我已经删除了所有的类(MainPage.xaml和它的.cs文件),并使用Add作为链接链接了父项目的MainPage.xaml和.cs文件。



在我的子项目中,我想使用此项目的资源文件(如AppResources.resx)运行项目。例如,我想从小程序中从AppResources.resx打印一个自定义的字符串值,我在xaml中使用这个代码。

 < TextBlock x:Name =TxtblkHorizo​​ntalAlignment =LeftMargin =56,147,0,0Grid.Row =1TextWrapping =Wrap
Text ={Binding Path = LocalizedResources .ApplicationName,Source = {StaticResource LocalizedStrings}}
VerticalAlignment =TopHeight =87Width =155/>

上述代码打印子项目的名称。



我想实现这个将代码放入我的.cs文件。使用以下语句我可以这样做,

  Txtblk.Text = AppResources.ApplicationTitle; 

但是要做到这一点,我必须添加父项目资源,并返回父项目的资源值。



我想使用绑定原则,如

  Binding b =新的Binding(); 
b.Source = LocalizedStrings; //如何将它设置为{StaticResource LocalizedStrings}
b.Path = new PropertyPath(LocalizedResources.ApplicationName);
b.Mode = BindingMode.OneWay;
Txtblk.SetBinding(TextBlock.TextProperty,b);

我该如何使这项工作?



谢谢。

解决方案

解决方案是为每个项目提供2个资源文件。一个在所有项目之间共享/链接,然后是每个项目唯一的项目。然后提供不同的名称,然后您可以在每个位置参考您想要的具体的名称。


I have a Parent project and a child project. In the child project I have removed all the classes (MainPage.xaml and it's .cs file) and have linked the Parent Project's MainPage.xaml and it's .cs file using Add as a link.

In my child Project I want to run the project using this project's resource files(like AppResources.resx). For example, I want to print a customised string value from AppResources.resx from child project, I use this code in my xaml..

<TextBlock x:Name="Txtblk" HorizontalAlignment="Left" Margin="56,147,0,0" Grid.Row="1" TextWrapping="Wrap" 
                  Text="{Binding Path=LocalizedResources.ApplicationName, Source={StaticResource LocalizedStrings}}"  
                   VerticalAlignment="Top" Height="87" Width="155"/>

The above code prints child Project's name.

I would like to achieve this bringing the code into my .cs file. Using the below statement I can do that ,

Txtblk.Text = AppResources.ApplicationTitle;

But to do this, I have to add Parent Projects resources and it returns Parent Project's resource values...

I want to use binding principle like

Binding b = new Binding();
b.Source = LocalizedStrings; // How do I set this to {StaticResource LocalizedStrings}
b.Path = new PropertyPath("LocalizedResources.ApplicationName");
b.Mode = BindingMode.OneWay;
Txtblk.SetBinding(TextBlock.TextProperty, b);

How do I make this work?

Thanks.

解决方案

The solution is to have 2 resource files for each project. One that is shared/linked across all projects and then one that is unique to each project. Give then different names and then you can refer to the specific one you want in each location.

这篇关于在共享类中如何使用本地资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆