[UWP]xaml中自定义附加属性使用方法的注意项

---恢复内容开始---

随笔小记,欢迎指正

在UWP平台上做WVVM的时候,想针对ListBox的SelectionChanged事件定义一个自定义的命令,于是使用自定义附加属性的方式。可是最后自定义附加属性SelectionChangedCommand写好了,却不知道怎么在XAML中使用。

我的自定义属性如下:

namespace SelectionChangedCommand.Services
{
    public static class SelectionChangedBehavior
    {
        public static readonly DependencyProperty SelectionChangedCommandProperty = DependencyProperty.Register("SelectionChangedCommand", typeof(ICommand), typeof(SelectionChangedBehavior), new PropertyMetadata(null, SelectionChangedPropertyCallBack));

        public static void SetSelectionChangedCommand(UIElement element, ICommand value)
        {
            element.SetValue(SelectionChangedCommandProperty, value);
        }
        public static ICommand GetSelectionChangedCommand(UIElement element)
        {
            return (ICommand)element.GetValue(SelectionChangedCommandProperty);
        }


        public static void SelectionChangedPropertyCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ListBox listBox = (ListBox)d;
            listBox.SelectionChanged += ListBox_SelectionChanged;
        }

        private static void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = (ListBox)sender;
            GetSelectionChangedCommand(listBox).Execute(listBox.SelectedItem);
        }
    }
}

 

于是百度了一番,看到在Wpf中是如下写法:

xmlns:aqua="clr-namespace:AquariumObjects;assembly=AquariumLibrary"

 于是我便模仿着写了如下代码

xmlns:my="clr-namespace:SelectionChangedCommand.Services;assembly=SelectionChangedCommand"

其中“SelectionChangedCommand”是我的程序集名称。

然后再一个ListBox中做了如下引用

<ListBox my:SelectionChangedBehavior.SelectionChangedCommand="{Binding SelectionChangedCommand}" ></ListBox>

 

结果提示我存在未知的可附加成员“SelectionChangedBehavior.SelectionChangedCommand",所以我就把上面的命名空间改成了

xmlns:my="using:SelectionChangedCommand.Services"

 

结果就可以了。

那么clr-namespace和using之间到底有什么区别呢?

目前在网上查到:silvelight中是:clr-namespace.  windows8中改成了using.

 

转载于:https://www.cnblogs.com/cjw1115/p/5052836.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以为你提供一个在UWP项目写自定义卡片组件的示例。是一个基本的示例: ```csharpusing Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Windows.UI.Xaml; namespace CustomCardComponent { public partial class CardComponent : { public CardComponent() this.InitializeComponent(); } // 自定义属性 public string Title { get { return titleLabel.Text; } set { titleLabel.Text = value; } } public string Description { get { return descriptionTextBlock.Text; } set { descriptionTextBlock.Text = value; } } public ImageSource Image { get { return image.Source; } set { image.Source = value; } } } } ``` 在XAML文件,你可以使用以下代码来定义卡片组件的外观和布局: ```xaml <UserControl x:Class="CustomCardComponent.CardComponent" ... > <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock x:Name="titleLabel" Grid.Row="0" FontSize="16" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center"/> <Grid Grid.Row="1"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Image x:Name="image" Grid.Row="0" Stretch="UniformToFill"/> <TextBlock x:Name="descriptionTextBlock" Grid.Row="1" TextWrapping="Wrap" Margin="5"/> </Grid> </Grid> </UserControl> ``` 通过使用上述代码,你可以在UWP项目创建和使用定义的卡片组件。你可以进一步根据你的需求来定制和扩展该组件。希望这可以帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值