gpt4 book ai didi

c# - 如何制作 UI-MarkupExtension

转载 作者:太空狗 更新时间:2023-10-29 20:35:46 24 4
gpt4 key购买 nike

我有一个简单的 UIElement,我想把它变成一个 MarkupExtension:

[MarkupExtensionReturnType(typeof(FrameworkElement))]
public class PinkRectangle : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new Rectangle {Height = 100, Width = 300, Fill = Brushes.HotPink };
}
}

它在大多数情况下都非常有效。唯一的异常(exception)是在列表中:

<local:WindowEx x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.winfx/200x/xaml"
xmlns:local="clr-namespace:WpfApp1"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
MyProperty="{Binding local:PinkRectangle}"> <!--this one works.-->
<local:WindowsEx.MyList>
<!--<Grid/> If I comment this line in, it works-->
<local:PinkRectangle/>
</local:WindowsEx.MyList>

<ContentPresenter Content="{Binding MyProperty}"/>
</local:WindowEx>

Collection Syntax ,它说:

If the type of a property is a collection, then the inferred collection type does not need to be specified in the markup as an object element. Instead, the elements that are intended to become the items in the collection are specified as one or more child elements of the property element. Each such item is evaluated to an object during loading and added to the collection by calling the Add method of the implied collection.

但是,xaml 将上面的语法解释为 MyList = PinkRectangle 而不是 MyList.Add(PinkRectangle) 但是如果我先放入一个 Grid,它会调用 MyList.Add () 两者都正确。 告诉 xaml 在这两种情况下都调用 MyList.Add() 的正确语法是什么?

这是创建 Minimal, Reproducable Example 的其余代码:

namespace WpfApp1
{
// I use this class to directly set a few unusual properties directly in xaml.
public class WindowEx : Window
{
//If I remove the set property, the error goes away, but I need the setter.
public ObservableCollection<object> MyList {get; set; } = new ObservableCollection();

public object MyProperty
{
get { return GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(nameof(MyProperty), typeof(object), typeof(MainWindow), new PropertyMetaData(0));
}

public partial class MainWindow : WindowEx
{
public MainWindow()
{
InitializeComponent();
}
}
}

- 编辑-

我发现如果我从 MyList 中删除 set{ },问题就会消失,因为 xaml 不再认为有 setter,但最终我需要能够设置 MyList... .

最佳答案

可怜的 XAML 解析器真的对这一切感到困惑......:O)通过消除歧义来帮助它:在你的 XAML 中显式实例化 MyList

enter image description here

XAML:

<local:UserControlEx x:Class="WpfApp14.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp14"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="450">

<local:UserControlEx.MyList>
<local:ObjectCollection>
<local:CoolBlueRectangle/>
<local:CoolBlueRectangle/>
<local:CoolBlueRectangle/>
<local:CoolBlueRectangle/>
<local:CoolBlueRectangle/>
</local:ObjectCollection>
</local:UserControlEx.MyList>

<Grid>
<ItemsControl HorizontalAlignment="Left"
ItemsSource="{Binding MyList}"/>
</Grid>

</local:UserControlEx>

在哪里,

public class ObjectCollection : ObservableCollection<object>
{
}

顺便说一句,命名约定是您的标记类定义应使用Extension 后缀。

public class CoolBlueRectangleExtension : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
}
}

关于c# - 如何制作 UI-MarkupExtension,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57119469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com