In UWP, what is the equivalent of DependencyType property of DependencyProperty Class of WPF?

我是研究僧i 提交于 2019-12-11 16:13:34

问题


After migrating a WPF class library to a UWP class library, the following code is throwing an error. The PropertyType property of DependencyProperty class worked in WPF. I tried to get help from Dependency properties overview of similar class in UWP and this online article but got a bit confused.

What I'm missing here and how can I make it work?

Code snippet [error occurs at first line of the method]:

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)
    {
    }
}

Error:

'DependencyProperty' does not contain a definition for 'PropertyType' and no accessible extension method 'PropertyType' accepting a first argument of type 'DependencyProperty' could be found (are you missing a using directive or an assembly reference?)

Snapshot of "ALL" the packages installed:


回答1:


Following is a simple example of how you can use DependencyProperty in UWP .

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

In the above example we are binding the dependency property welcomeText which we define in the code behind (C#) to a TextBlock.

Please note, ElementName=loginPage is the page name that we define in the XAML.

Hope this helps.


EDIT 1:

From what I can understand from your code you are trying to get the PropertyType value in order to convert it to a different type.

For this requirement you can do something like this:

In the following example we have a custom value converter which converts Length of a string to Visibility, in other words, return Visibility based on the length of string it receives for conversion, while also checking if the type of value provided is of the type 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>

Here, the second TextBlock's visibility is based on the length of text of flyoutTxt.

C#

Custom Converter class to convert Length to Visibility :

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

Please note, there are no changes required in the property dependency defined above.



来源:https://stackoverflow.com/questions/55437264/in-uwp-what-is-the-equivalent-of-dependencytype-property-of-dependencyproperty

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