【问题标题】:In UWP, what is the equivalent of DependencyType property of DependencyProperty Class of WPF?在 UWP 中,WPF 的 DependencyProperty 类的 DependencyType 属性等价于什么?
【发布时间】:2019-08-21 13:34:14
【问题描述】:

将 WPF 类库迁移到 UWP 类库后,以下代码抛出错误。 DependencyProperty classPropertyType 属性在 WPF 中工作。我试图从 UWP 中 similar 类的 Dependency properties overviewthis 在线文章中获得帮助,但有点困惑。

我在这里缺少什么以及如何使它起作用?

代码sn -p [方法第一行出错]:

using Windows.UI.Xaml;
using System.Reflection;
using Windows.UI.Xaml.Documents;
using System.ComponentModel;
....

private static void SetPropertyValue(XmlElement xamlElement, DependencyProperty property, string stringValue)
{
    TypeConverter typeConverter TypeDescriptor.GetConverter(property.PropertyType);
    try
    {
        object convertedValue = typeConverter.ConvertFromInvariantString(stringValue);

        if (convertedValue != null)
        {
            xamlElement.SetAttribute(property.Name, stringValue);
        }
    }
    catch(Exception)
    {
    }
}

错误

“DependencyProperty”不包含“PropertyType”的定义,并且找不到接受“DependencyProperty”类型的第一个参数的可访问扩展方法“PropertyType”(您是否缺少 using 指令或程序集引用?)

安装的“所有”包的快照:

【问题讨论】:

标签: c# wpf uwp dependency-properties


【解决方案1】:

以下是如何在 UWP 中使用 DependencyProperty 的简单示例。

XAML

<Page x:Name="loginPage"
... >

<TextBlock Text="{Binding welcomeText,ElementName=loginPage}"></TextBlock>

C#

using Windows.UI.Xaml;
...
public string welcomeText
{
     get { return (string)GetValue(welcomeTextProperty); }
     set { SetValue(welcomeTextProperty, value); }
}

// Using a DependencyProperty as the backing store for welcomeText.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty welcomeTextProperty =
        DependencyProperty.Register("welcomeText", typeof(string), typeof(LoginPage), null);

在上面的示例中,我们将在 (C#) 后面的代码中定义的依赖属性 welcomeText 绑定到 TextBlock

请注意,ElementName=loginPage 是我们在 XAML 中定义的页面名称。

希望这会有所帮助。


编辑 1:

根据我从您的代码中了解到的情况,您正在尝试获取 PropertyType 值以便将其转换为不同的类型。

对于这个要求,你可以这样做:

在下面的示例中,我们有一个自定义值转换器,它将字符串的长度转换为Visibility,换句话说,根据接收到的用于转换的字符串长度返回Visibility,同时还检查字符串的类型是否为value 提供的类型是 string

XAML

<Page x:Name="loginPage"
 xmlns:converters="using:projectName.converters"
... >
<Page.Resources>
    <converters:LengthToVisibilityConverter x:Key="lengthToVisibilityKey"></converters:LengthToVisibilityConverter>
</Page.Resources>
...
<TextBlock x:Name="flyoutTxt" Text="{Binding welcomeText,ElementName=loginPage}"></TextBlock>
<TextBlock Text="Has some text" Visibility="{Binding Path=Text,ElementName=flyoutTxt,Converter={StaticResource lengthToVisibilityKey}}"></TextBlock>

这里,第二个TextBlock的可见性是基于flyoutTxt的文本长度。

C#

自定义转换器类将长度转换为可见性:

class LengthToVisibilityConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {   //checking if type of "value" is String and its length
        if(value != null && value.GetType() == typeof(string) && 
           value.ToString().Length > 0)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }

    }

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

请注意,上面定义的属性依赖项不需要更改。

【讨论】:

  • 那么,我们如何以编程方式获取上述示例的 DependenceProperty welcomeTextPropertyType 值?请记住,我的代码中的错误在我的代码的以下行中,抱怨“DependencyProperty”不包含“PropertyType”的定义:TypeConverter typeConverter TypeDescriptor.GetConverter(property.PropertyType);
  • 我可能应该改写这篇文章。问题是WPF 中的DependenceProperty 类有一个名为PropertyType 的属性。但是UWP 中的DependenceProperty 类没有这样的属性。那么,我们如何以编程方式处理这种情况呢?例如,如果WPF 中的类Person 具有属性LastName,则可以通过Person prs = new Person(), prs.LastName 访问它。但是UWP 中的Person 类没有LastName 属性。那么,当我们将WPF 类库转换为UWP 库时,我们如何处理UWP 中的Person 类的LastName 属性?
  • 好的..我现在明白了,我误解了这个问题..我不确定你是否能够替换 PropertyType,因为 UWP 中的 DependencyProperty 来自 Windows.Ui。 xaml 与 WPF 中一样,它来自不同的命名空间。
猜你喜欢
  • 2019-01-16
  • 2012-01-09
  • 1970-01-01
  • 2019-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多