为什么我不能动态创建一个拇指? (Wpf - C#) [英] Why cant I create a thumb dynamically? (Wpf - C#)

查看:80
本文介绍了为什么我不能动态创建一个拇指? (Wpf - C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

this is my code

public MainWindow()
{
    InitializeComponent();
    StringBuilder sb = new StringBuilder();
    sb.Append(@"<Thumb x:Name='thumb1' Canvas.Left='0' Canvas.Top='0' Canvas.ZIndex='99' DragDelta='Thumb_DragDelta' >
        < Thumb.Template >
            < ControlTemplate >
                < Image Source = 'images/table1.jpg' Name = 't1' Stretch = 'None' />
            </ ControlTemplate >
        </ Thumb.Template >
    </ Thumb > ");
    UIElement myThumb = (UIElement)XamlReader.Parse(sb + "");
    Canvas.Children.Add(myThumb);



我得到这个错误:



异常抛出:'System.Windows.Markup。 PresentationFramework.dll中的XamlParseException'



附加信息:'x'是未声明的前缀。第1行,第8位。



我尝试过:



我尝试在c#中创建一些代码,在我的应用程序中放置一些xaml控件,这样我就可以将它放在foreach循环中并检查数据库。


I GET THIS ERRORMESSAGE :

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll

Additional information: 'x' is an undeclared prefix. Line 1, position 8.

What I have tried:

I try to create some code in c# that will put some xaml controls on my application so i can put it in a foreach loop and check the database.

推荐答案

从错误我怀疑问题是你没有指定与x前缀对应的XML命名空间。您可以尝试以下方法:

From the error I suspect the problem is that you haven't specified the the XML namespace corresponding to the x prefix. You could try something like:
public MainWindow()
{
  InitializeComponent();
  const string thumbXaml = 
    "<Thumb x:Name='thumb1' 
            xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
            xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
            Canvas.Left='0'
            Canvas.Top='0'
            Canvas.ZIndex='99'
            DragDelta='Thumb_DragDelta' >
        <Thumb.Template>
            <ControlTemplate>
                <Image Source = 'images/table1.jpg' Name = 't1' Stretch = 'None' />
            </ControlTemplate>
        </Thumb.Template>
     </Thumb>";
    UIElement myThumb = (UIElement)XamlReader.Parse(thumbXaml);
    Canvas.Children.Add(myThumb); // the name of your Canvas element is "Canvas"??



您当然需要 xmlns:x = 属性,还可能需要 xmlns = 属性。我怀疑它仍然会失败,因为它无法知道上下文,所以它可以找到 Thumb_Drag_Delta 事件处理程序方法,我怀疑它在<$ c $中c> MainWindow 代码背后。

(以及图片中的名称属性元素可能应该是 x:名称。)



好​​的。首先,由于这一切都是静态的,为什么不将它放入 MainWindow 的XAML文件中?



但是,也许这只是简化为例......足够公平。



在代码中创建控件,设置适当的值然后挂钩'全部。


You certainly need the xmlns:x= attribute, and probably also the xmlns= attribute. I suspect it will still fail because it has no way of knowing about the context so it can locate the Thumb_Drag_Delta event handler method that, I suspect, is in the MainWindow code behind.
(And the "Name" attribute in the Image element probably should be "x:Name".)

OK. First of all, since this is all static, why not just put it into the XAML file for the MainWindow?

But, maybe this is just "simplified for an example"...fair enough.

Create the controls in code, set the appropriate values and then hook 'em all up.

public MainWindow()
{
  InitializeComponent();
  Thumb th = new Thumb();
  th.AddHandler(Thumb.DragDeltaEvent, this.Thumb_DragDelta);
  ControlTemplate thCt = new ControlTemplate();
  // fill in the ControlTemplate here....
  th.Template = thCt;
  myCanvas.Children.Add(th);  // I renamed this for disambiguation re: the Canvas class itself.
  th.SetValue(Canvas.LeftProperty, 0);  // MTH: Fixed the property names...
  th.SetValue(Canvas.TopProperty, 0);
  th.SetValue(Canvas.ZIndexProperty, 99);
  // etc...



(警告:我没有尝试任何一个......!)


(Caveat: I didn't try any of this...!)


这篇关于为什么我不能动态创建一个拇指? (Wpf - C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆