UWP 用户控件依赖属性不起作用

Jer*_*ker 2 .net c# xaml xamarin uwp

我正在学习 UWP 用户控制并面临以下代码:

<Page
    x:Class="LearningUWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LearningUWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <StackPanel>
        <local:MyUserControl Username="aaa" Password="bbb" fillcolor="Blue" />
    </StackPanel>
</Page>
Run Code Online (Sandbox Code Playgroud)

我定义了一个具有三个依赖属性的用户控件:用户名、密码和填充颜色。下面的代码显示了我的用户控件 xaml

<UserControl
    x:Name="myctrl"
    x:Class="LearningUWP.MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LearningUWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    >
    <RelativePanel>
        <Rectangle Fill="{Binding fillcolor, ElementName=myctrl}"  x:Name="Rec" Height="100"   RelativePanel.AlignRightWithPanel="True" />
        <TextBlock Text="Username_lbl" RelativePanel.AlignRightWithPanel="True" TextAlignment="Center" RelativePanel.Below="Rec" x:Name="Username_lb" Margin="0,50,0,0" RelativePanel.AlignLeftWithPanel="True" />
        <TextBox x:Name="Username_input" RelativePanel.Below="Username_lb" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignLeftWithPanel="True" Margin="0,20,0,0" Text="{Binding Username, ElementName=myctrl}"  ></TextBox>
        <TextBlock Text="Password_lbl" x:Name="Password_lb" RelativePanel.AlignRightWithPanel="True" TextAlignment="Center" RelativePanel.Below="Username_input" RelativePanel.AlignLeftWithPanel="True" Margin="0,20,0,0" ></TextBlock>
        <TextBox x:Name="Password_input" RelativePanel.Below="Password_lb" RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignLeftWithPanel="True" Margin="0,20,0,0" Text="{Binding Password, ElementName=myctrl}" ></TextBox>
    </RelativePanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

和隐藏代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace LearningUWP
{
    public sealed partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            this.InitializeComponent();
        }
        public string Username
        {
            get { return (string)GetValue(UsernameProperty); }
            set { SetValue(UsernameProperty, value); }
        }
        public static readonly DependencyProperty UsernameProperty =
            DependencyProperty.Register("Username", typeof(string), typeof(MyUserControl), null);
        public string Password
        {
            get { return (string)GetValue(PasswordProperty); }
            set { SetValue(PasswordProperty, value);         }
        }
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(MyUserControl), null);
        public string fillcolor
        {
            get { return (string)GetValue(fillcolorProperty); }
            set { SetValue(fillcolorProperty, value); }
        }
        public static readonly DependencyProperty fillcolorProperty =
            DependencyProperty.Register("fillcolor", typeof(string), typeof(MyUserControl), null);
    }
}
Run Code Online (Sandbox Code Playgroud)

用户名和密码正常,我可以在屏幕上看到“aaa”和“bbb”,但颜色不起作用。怎么解决呢?

更新1

我将 fillcolor 属性修改为以下代码:

public Brush fillcolor
        {
            get { return (Brush)GetValue(fillcolorProperty); }
            set { SetValue(fillcolorProperty, value); }
        }
        public static readonly DependencyProperty fillcolorProperty =
            DependencyProperty.Register("fillcolor", typeof(Brush), typeof(MyUserControl), null);
Run Code Online (Sandbox Code Playgroud)

但它仍然不起作用。

更新2

我更新了 MainPage.xaml,如下所示:

<Page
    x:Class="LearningUWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LearningUWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Page.Resources>
        <SolidColorBrush x:Key="MyBlueColor" Color="#0000FF"/>
    </Page.Resources>
    <StackPanel>
        <local:MyUserControl Username="aaa" Password="bbb" fillcolor="{StaticResource MyBlueColor}" />
    </StackPanel>
</Page>
Run Code Online (Sandbox Code Playgroud)

但它仍然不起作用。

Bru*_*uno 5

您的绑定似乎是正确的,您的问题出在Type“fillcolor”属性中。您将其绑定为字符串,而您应该将其绑定为画笔

 public Brush fillcolor
        {
            get { return (Brush)GetValue(fillcolorProperty); }
            set { SetValue(fillcolorProperty, value); }
        }
 public static readonly DependencyProperty fillcolorProperty =
            DependencyProperty.Register("fillcolor", typeof(Brush), typeof(MyUserControl), null);
Run Code Online (Sandbox Code Playgroud)

Fill 属性需要 Brush 才能正常工作,如您在此处Shape.Fill中看到的那样

我忘了提到,在这种情况下,您不需要指定元素名称,因为您将后面的代码用作“ViewModel”,因此,下面的代码将为您解决问题:

<Rectangle Fill="{x:Bind fillcolor}"  x:Name="Rec" Height="100"   RelativePanel.AlignRightWithPanel="True" />
Run Code Online (Sandbox Code Playgroud)

此外,您需要将Brush页面资源中的 指定为纯色画笔,因此您可以编写:

<Page
    x:Class="LearningUWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LearningUWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.Resources>
     <SolidColorBrush x:Key="MyBlueColor" Color="#0000FF"/> <!-- or Blue -->
    </Page.Resources>
    <StackPanel>
        <local:MyUserControl Username="aaa" Password="bbb" fillcolor="{StaticResource MyBlueColor}" />
    </StackPanel>
</Page>
Run Code Online (Sandbox Code Playgroud)

并且,根据difference- Between-binding-and-xbind 我们需要使用x:Bind不支持框架元素的Binding。