简体   繁体   English

在 Silverlight 中使用 C# 代替 XAML

[英]Using C# instead of XAML in Silverlight

Is XAML really necessary? XAML 真的有必要吗? If someone really wanted to, could everything displayed on a Silverlight page be declared at runtime in the C# code?如果有人真的想要,是否可以在运行时在 C# 代码中声明 Silverlight 页面上显示的所有内容? VISIFire has example code where they set up one of their classes, chart, in the C# code and then simply add it to the user interface with a simple C# call: VISIFire 有示例代码,他们在 C# 代码中设置了他们的类之一,图表,然后通过简单的 C# 调用将其添加到用户界面:

LayoutRoot.Children.Add(chart);

How would I do a similar creation with a class I already have defined in the XAML file but because of other reasons I need to declare it at runtime?如何使用我已经在 XAML 文件中定义的 class 进行类似的创建,但由于其他原因,我需要在运行时声明它? If I could use it as it is in XAML I think it would work because there are no errors with its display.如果我可以像在 XAML 中那样使用它,我认为它会起作用,因为它的显示没有错误。 It is declared like this:它是这样声明的:

        <views:PrescriberPage01 x:Name="DashboardPrescriberPage01" 
    Visibility="Collapsed" SelectedProduct="{Binding SelectedProduct,
 Mode=TwoWay}"Grid.Row ="0"/>

How would I declare that in C# code?我将如何在 C# 代码中声明它?

Is XAML really necessary? XAML 真的有必要吗?

I suppose no, you can probably do everything that you can do in XAML in code.我想不,您可能可以在代码中执行 XAML 中可以执行的所有操作。 But XAML is really, really a much, much more convenient way to go.但是 XAML 真的是一个非常非常方便的 go 方法。 And I think few would dissagree with this.我认为很少有人会不同意这一点。

How would I declare that in C# code?我将如何在 C# 代码中声明它?

You can create bindings in code.您可以在代码中创建绑定。 Example from MSDN .来自 MSDN 的示例 In your situation it would be something like:在您的情况下,它将类似于:

PrescriberPage01 page = new PrescriberPage01();
page.Visibility = Visibility.Collapsed;

Binding myBinding = new Binding("SelectedProduct");
myBinding.Mode = BindingMode.TwoWay;
myBinding.Source = this; // or whatever object carrying the property
                         // "SelectedProduct" that you bind against.

page.SetBinding(PrescriberPage01.SelectedProduct, myBinding);
LayoutRoot.Children.Add(page);

XAML is preferable to describe user interface, in declarative way and I recommend use XAML without code behind and also I would recommend look at MVVM pattern XAML 更适合以声明性方式描述用户界面,我建议使用 XAML 没有代码后面,我也建议查看 MVVM 模式

I would rather say "Is C# really necessary for GUIs when I can use XAML"?我宁愿说“当我可以使用 XAML 时,C# 对于 GUI 真的有必要吗”? XAML is really good and was explicitly design for GUI. XAML 非常好,并且是专门为 GUI 设计的。

You have two options here.你在这里有两个选择。 Load your XAML dynamicly .动态加载您的XAML

Or create it in code.或者在代码中创建它。 (other posted the solution while i was typing AGAIN; ;) ) (其他人在我再次输入时发布了解决方案;;))

Simple controls are easy but... you might also want some styling done to those objects + you might want an animation or two... this is already(mostly) provided in xaml... and it is uber easy... If u hate xaml so much you might want to think about "Blend";p简单的控制很容易,但是...您可能还希望对这些对象进行一些样式设置+您可能需要一个或两个 animation...这已经(大部分)在 xaml 中提供...而且非常容易...如果你非常讨厌 xaml 你可能想考虑“混合”;p

One specific example of something you can't create without Xaml is a DataTemplate.没有 Xaml 就无法创建的一个具体示例是 DataTemplate。

You can write code which will create a DataTemplate from a XAML string, but you can't add children to a newed up DataTemplate directly in c#.您可以编写代码,从 XAML 字符串创建 DataTemplate,但您不能直接在 c# 中将子项添加到新的 DataTemplate。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM