UWP和绑定到不同项目中的静态类

本文关键字:项目 静态类 绑定 UWP | 更新日期: 2023-09-27 18:12:38

我在一个公共项目中有一个静态类,我有我所有的属性和所有的文本(例如,我所有的标题)

我想知道如何将我的TextBlock Text绑定到这个值。

I try Text={x:Static…}但是没有找到Static

谢谢

UWP和绑定到不同项目中的静态类

{x:static…}在UWP中不存在。

仍然可以做类似的事情,但是类本身不能是静态的。类中的属性可以是静态的,但需要创建该类的实例。因此,您需要请求对核心库进行更改。

然后将该类声明为资源,并将其用作绑定的源。我建议你在全局可用的地方声明资源,比如app.xaml。

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

您可以使用静态值返回设置Converter:

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

现在根据你的DataContext你应该写你的返回逻辑:

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();
    }
}