1

目前我正在开发一个 UI 库,它应该包含自定义 UI 元素、布局和对话框,以便拥有一致且可重用的 UI 集合。

在我的项目中,目前的结构如下:

  • UILib(库的共享代码)
  • UILib.Droid(包含自定义渲染等的android库)
  • UILib.iOS(包含自定义渲染器等的 iOS 库)
  • UILibDemo(使用库的演示应用程序的共享代码)
  • UILibDemo.Droid(Android 演示应用程序)
  • UILibDemo.iOS(演示应用程序 iOS)

在我的库中,我有一个类“LoadingDialog”,到目前为止工作正常。但是我的问题是我现在正在尝试定义一种样式以从演示应用程序的共享代码 (App.xaml) 中更改 LoadingDialog 的某些属性:

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:dialogs="clr-namespace:UILib.Dialogs;assembly=UILib"
         x:Class="BetterUIDemo.App">
<Application.Resources>
    <ResourceDictionary>
        <x:String x:Key="DefaultFontLight">OpenSans-Light.ttf</x:String>
        <Color x:Key="ThemeColor">#f08c00</Color>
        <Color x:Key="BackgroundColor">#37474f</Color>

        <Style x:Key="LoadingDialogStyle" TargetType="dialogs:LoadingDialog">
            <Setter Property="ThrobberBackgroundColor" Value="White" />
            <Setter Property="LabelFontFamily" Value="{DynamicResource DefaultFontLight}" />
            <Setter Property="LabelColor" Value="{DynamicResource ThemeColor}" />
            <Setter Property="LoadingText" Value="Lade Daten ..." />
        </Style>
    </ResourceDictionary>
</Application.Resources>

我的 LoadingDialog 类也包含这些属性:

#region Properties
    public static BindableProperty ThrobberBackgroundColorProperty = BindableProperty.Create("ThrobberBackgroundColor", typeof(Color), typeof(Color), Color.Black);
    public static BindableProperty FontFamilyProperty = BindableProperty.Create("FontFamily", typeof(string), typeof(string), Font.Default.FontFamily);
    public static BindableProperty LabelColorProperty = BindableProperty.Create("LabelColor", typeof(Color), typeof(Color), Color.Black);
    public static BindableProperty LoadingTextProperty = BindableProperty.Create("LoadingText", typeof(string), typeof(string), "Loading ...");

    public string LabelFontFamily
    {
        get { return (string)GetValue(FontFamilyProperty); }
        set { SetValue(FontFamilyProperty, value); }
    }

    public Color ThrobberBackgroundColor
    {
        get { return (Color)GetValue(ThrobberBackgroundColorProperty); }
        set { SetValue(ThrobberBackgroundColorProperty, value); }
    }

    public string LoadingText
    {
        get { return (string)GetValue(LoadingTextProperty); }
        set { SetValue(LoadingTextProperty, value); }
    }

    public Color LabelColor
    {
        get { return (Color)GetValue(LabelColorProperty); }
        set { SetValue(LabelColorProperty, value); }
    }

    #endregion

但是,当我尝试编译演示应用程序时,出现以下错误:

严重性代码描述项目文件行抑制状态错误位置 14:13。无法在 LoadingDialog UILibDemo C:\Work\UILib\UILibDemo\UILibDemo\App.xaml 14 上解析 LabelFontFamily

有什么建议我可能做错了吗?

4

2 回答 2

1

我认为这可能只是导致您的错误的命名事物。

可绑定属性使用一些“魔术”,其中Property namebindable property需要命名相同的东西,但可绑定属性的末尾有“属性一词

注意你BindableProperty是如何被调用的FontFamily

将其更改为以下内容应该可以解决您的错误

public static BindableProperty LabelFontFamilyProperty = BindableProperty.Create("LabelFontFamily", typeof(string), typeof(string), Font.Default.FontFamily);

public string LabelFontFamily
{
   get { return (string)GetValue(FontFamilyProperty); }
   set { SetValue(FontFamilyProperty, value); }
}
于 2018-07-24T09:32:09.157 回答
0

解决了。供进一步参考:

问题是注册为“FontFamily”的 BindableProperty 与实际属性不匹配

public string LabelFontFamily
{
    get { return (string)GetValue(FontFamilyProperty); }
    set { SetValue(FontFamilyProperty, value); }
}

将属性更改为

公共字符串 FontFamily

解决了这个问题。

于 2018-07-24T09:31:17.483 回答