Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# uwp中的矩形到拇指模板_C#_Xaml_Uwp - Fatal编程技术网

C# uwp中的矩形到拇指模板

C# uwp中的矩形到拇指模板,c#,xaml,uwp,C#,Xaml,Uwp,我正在动态创建一个矩形,该矩形必须添加到Thumb模板中。在Wpf中,我执行以下操作。但在UWP中,它表示找不到PresentationFramework.dll的FrameworkElementFactory部分。但是当我添加对dll的引用时,它会给出一系列错误。如何创建矩形并将其指定给拇指。请帮忙 WPF中的代码 public void RectangleToThumb() { Rectangle r = new Rectangle();

我正在动态创建一个矩形,该矩形必须添加到Thumb模板中。在Wpf中,我执行以下操作。但在UWP中,它表示找不到PresentationFramework.dll的FrameworkElementFactory部分。但是当我添加对dll的引用时,它会给出一系列错误。如何创建矩形并将其指定给拇指。请帮忙

WPF中的代码

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