UWP and Binding to static class in different project

橙三吉。 提交于 2019-12-11 08:11:34

问题


I have a static class in a common project where I have all my property with all my text (for example, all my titles)

I want to know how I can bind my TextBlock Text to this value.

I tried Text={x:Static...} but Static is not found.

Thanks


回答1:


{x:static...} is not present in UWP.

You can still do something similar, but the class itself must not be static. The properties in the class can be static but you need to create an instance of that class. So you will need to ask for a change in the core lib.

Then you declare that class as a resource and use that as the source of your Bindings. I recommend you declare the resource where it's globally available, like app.xaml.

<Application.Resources>
    <lib:Class1 x:Key="c1"/>
</Application.Resources>
...
<TextBlock Text="{Binding Source={StaticResource c1}, Path=Text1}" />



回答2:


You may set Converterwith your static value return:

  <TextBlock Text="{Binding Converter={StaticResource GetStatiField}}"/>

Now according to your DataContext you should write your return logic:

public sealed class GetStatiField: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        //value is a type of your TextBlock.DataContext
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}


来源:https://stackoverflow.com/questions/39978461/uwp-and-binding-to-static-class-in-different-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!