简体   繁体   English

通过C#插入XAML元素

[英]Inserting XAML elements through C#

I'm just starting with Universal Windows Platform and actually C# as well (I've got a C background) and just trying to get my feet wet with a basic accounting app meant to keep track of a checkbook. 我只是从通用Windows平台开始,实际上也从C#(我有C背景)开始,只是想通过一个用于跟踪支票簿的基本会计应用程序来了解自己。 The way it works is simple, the user either presses + or - buttons and specifies the name of the transaction and the amount either credited or debited, hits ok and that transaction is inserted as a row. 它的工作方式很简单,用户按下+或-按钮并指定交易名称和贷记或借记金额,点击确定,然后将该交易作为行插入。

So it would look something like this 所以看起来像这样

Gas       ($20)  1/5/17   _delete_button_
Paycheck   $2400 1/3/17   _delete_button_

Total      $2380

.... ....

So each row has 2 editable text boxes (name and amount), one textfield for date, and one button to delete that row. 因此,每一行都有2个可编辑的文本框(名称和金额),一个用于日期的文本字段和一个用于删除该行的按钮。

Now my first instinct as a C programmer is just to create a struct with those variables and every time user inserts a row just populate the struct and push it on a stack. 现在,我作为C程序员的第一个本能只是用这些变量创建一个结构,并且每次用户插入一行时,只需填充该结构并将其压入堆栈。 However I just want to make sure that this is the best way to do this and I'm not missing some feature of XAML that would let me do this. 但是,我只想确保这是执行此操作的最佳方法,并且我不会丢失XAML的某些功能让我执行此操作。

"Now my first instinct as a C programmer is just to create a struct with those variables and every time user inserts a row just populate the struct and push it on a stack. However I just want to make sure that this is the best way to do this and I'm not missing some feature of XAML that would let me do this." “现在,作为C程序员,我的第一个本能只是用这些变量创建一个结构,并且每次用户插入一行时,只需填充该结构并将其压入堆栈。但是,我只想确保这是最好的方法做到这一点,我就不会错过XAML的某些功能,让我可以做到这一点。”

This question is dangerously close to not meeting the requirements for a good question because it is too broad, but I will answer anyway due to remembering the pain I went through with XAML when new. 这个问题过于笼统,很接近不能满足一个好问题的要求,但我仍然会回想一下,因为我记得当我刚接触XAML时所经历的痛苦。 Whether new or experienced (and I am at most in the "intermediate" category), it seems XAML has its bits that will always send you looking for help. 不论是新手还是经验丰富的人(而且我最多是“中级”类别),XAML似乎都有其特色,它总是会向您寻求帮助。 But I digress. 但是我离题了。

The standard is to create your model, then somehow connect (or "bind") the appropriate parts of the model to your view. 标准是创建模型,然后以某种方式将模型的适当部分连接(或“绑定”)到视图。

You would use C# (actually can use VB or F# or others also) for your model. 您将为模型使用C# (实际上也可以使用VB或F#或其他)。 This is where you would create your "struct" you are referring to. 在这里可以创建您要引用的“结构”。 This would define your individual transaction. 这将定义您的个人交易。 Then, create a collection of these transactions, again in your model. 然后,再次在模型中创建这些交易的集合。

In XAML , you bind to this collection. XAML中 ,您绑定到此集合。 You have a control (a visual element in your view) that displays all transactions. 您有一个显示所有交易的控件(视图中的可视元素)。 You have a control for specifying the name of a specific transaction, for selecting a transaction, for inserting, for deleting, etc. All of these controls interact with the model through the bindings. 您具有一个控件,用于指定特定事务的名称,选择事务,插入,删除等。所有这些控件都通过绑定与模型交互。

So, to answer your question, yes you are missing a feature of XAML. 因此,要回答您的问题, 是的,您缺少XAML功能。 It is called binding. 这称为绑定。 Not to be sarcastic but unfortunately, binding is a technology worthy of months of study. 并不是讽刺,而是不幸的是,绑定是一项值得数月研究的技术。 Sorry, but you will want to go back to C with no UI at first. 抱歉,但是您将首先想回到没有UI的C语言。 Keep at it! 继续吧! If you want to do the universal app thing, it is worth it. 如果您想做通用应用程序,那是值得的。 You may even find yourself liking (parts of) it. 您甚至可能会喜欢它的一部分。

Link to get thou started: 链接即可开始使用:

WPF and other XAML-related WPF和其他与XAML相关的

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

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