C# uwp中的矩形到拇指模板
我正在动态创建一个矩形,该矩形必须添加到Thumb模板中。在Wpf中,我执行以下操作。但在UWP中,它表示找不到PresentationFramework.dll的FrameworkElementFactory部分。但是当我添加对dll的引用时,它会给出一系列错误。如何创建矩形并将其指定给拇指。请帮忙 WPF中的代码C# uwp中的矩形到拇指模板,c#,xaml,uwp,C#,Xaml,Uwp,我正在动态创建一个矩形,该矩形必须添加到Thumb模板中。在Wpf中,我执行以下操作。但在UWP中,它表示找不到PresentationFramework.dll的FrameworkElementFactory部分。但是当我添加对dll的引用时,它会给出一系列错误。如何创建矩形并将其指定给拇指。请帮忙 WPF中的代码 public void RectangleToThumb() { Rectangle r = new Rectangle();
public void RectangleToThumb()
{
Rectangle r = new Rectangle();
r.Height = 80;
r.Width = 150;
r.Fill = Brushes.Red;
ControlTemplate t = new ControlTemplate();
var rectangle = new FrameworkElementFactory(typeof(Rectangle));
rectangle.SetValue(Rectangle.HeightProperty, r.Height);
rectangle.SetValue(Rectangle.WidthProperty, r.Width);
rectangle.SetValue(Rectangle.FillProperty, r.Fill);
t.VisualTree = rectangle;
Thumb th = new Thumb();
th.Template = t;
th.DragDelta += Th_DragDelta;
cnvDemo.Children.Add(th);
}
uwp中的矩形到拇指模板
FrameworkElementFactory
无法在UWP平台中支持。如果我们想要实现,我们需要将xaml字符串作为ControlTemplate
加载到类中,有关详细信息,请参考以下内容
public void RectangleToThumb()
{
Rectangle r = new Rectangle();
r.Height = 80;
r.Width = 150;
r.Fill = new SolidColorBrush(Colors.Red);
ControlTemplate t = CreateTemplate();
Thumb th = new Thumb();
th.Template = t;
RootGrid.Children.Add(th);
}
private static ControlTemplate CreateTemplate()
{
const string xaml = "<ControlTemplate TargetType='Thumb' xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Rectangle Width = '150' Height = '80' Fill = 'Red' /></ControlTemplate>";
var сt = (ControlTemplate)XamlReader.Load(xaml);
return сt;
}
public void RectangleToThumb()
{
矩形r=新矩形();
r、 高度=80;
r、 宽度=150;
r、 填充=新的SolidColorBrush(颜色为红色);
ControlTemplate t=CreateTemplate();
拇指th=新拇指();
th.模板=t;
RootGrid.Children.Add(th);
}
私有静态ControlTemplate CreateTemplate()
{
常量字符串xaml=“”;
varСt=(ControlTemplate)XamlReader.Load(xaml);
返回Сt;
}