如何在有效的方法中将xaml转换为代码? [英] How to convert xaml to code behind in effective method?

查看:71
本文介绍了如何在有效的方法中将xaml转换为代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在XAML中有这个代码,但我想把它翻译成代码。我已经在互联网上搜索如何从xaml转换为代码,但从我所看到的,xaml本身如何移动,旋转,颜色变化等方法有所不同。我们究竟能从xaml到后面的代码读取动画吗?

Hi all,

I have this code in XAML but I want to translate it to code behind. I already search on the internet how to convert from xaml to code, but from what I seen, there is some difference on how the xaml itself such as moving, rotate, color changing etc with different approach. How exactly we can read the animation from xaml to code behind?

<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.StrokeDashOffset)" Storyboard.TargetName="path19">
    <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
    <EasingDoubleKeyFrame KeyTime="0:0:1" Value="-14"/>
</DoubleAnimationUsingKeyFrames>





在代码背后做什么是第一步,然后按照xaml之后的代码进行操作?



What are first step in doing in code behind and follow by what code after that based from xaml?

推荐答案

我相信第一步是在线搜索以找到动画中使用的类离子。



我没试过这个,但以下是我认为它会起作用的方式。 :-)



I believe the first step is searching online to find the classes that are used in the animation.

I have not tried this, but the following is how I think it would work. :-)

DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames();
KeyTime start = new TimeSpan(0);
animation.KeyFrames.Add(new EasingDoubleKeyFrame(0, start));
KeyTime end = TimeSpan.FromSeconds(1);
animation.KeyFrames.Add(new EasingDoubleKeyFrame(-14,end));
            
[YourShapeMember].BeginAnimation(Shape.StrokeDashOffsetProperty, animation);


我想给你一般答案,一个实用的建议。



你总是可以重新实现任何给定的XAML行为来代码。很明显,反过来说不是真的。如果您了解XAML的工作原理,您可以理解它:构建使用XAML数据生成一些代码。然后,这个自动生成的代码将转到编译器,就像另一段代码一样。 (当然,你总是可以在运行时纯粹使用一些XAML。这与你的问题没有关系。)



所以,这是我的通用目的实用建议:假设你学会了如何在XAML中实现某些行为,但是你想要在代码中实现某些东西,基于这种XAML行为的东西,你不知道如何。您可以通过这种简单的方式轻松了解正在发生的事情:自动生成的文件在构建后保留;您将很容易找到它们:子目录obj是由build创建的,它是项目文件目录的子目录;并且所有自动生成的文件都在obj的目录结构中。



例如:假设您使用C#和XAML,并且您使用XAML创建三个类: Window 类WindowA和WindowB,还有一个 UserControl 类ControlC。然后,在自动生成的代码中,您将能够找到文件WindowA.g.cs,WindowB.g.cs和ControlC.g.cs。看看它们。



它们是用你的开发语言编写的文件(C#,VB.NET,C ++ / CLI,F#等)。打开然后看看他们做了什么。这样,您就可以了解会发生什么。通常,代码比你后面的代码中真正需要的代码更复杂,但是对于你来说,获得这个想法很重要:什么是FCL类以及用什么成员来实现你想要的东西。



-SA
I want to give you the very general answer and one practical advice.

You can always re-implement any given XAML behavior to code behind. Pretty obviously, the other way around is not true. You can understand it if you learn how XAML works: the build uses XAML data to generate some code behind. This auto-generated code then goes to a compiler, exactly as another other piece of code. (Of course, you can always use some XAML purely during runtime. This is just not the case related to your question.)

So, here is my general-purpose practical advice: suppose you learned how to achieve some behavior in XAML but you want to implement something in code behind, something based on this XAML behavior, and you don't know how. You can easily learn what's going on in this simple way: the auto-generated files are preserved after the build; you will easily find them: the sub-directory "obj" is created by build, it is a sub-directory of the directory of your project files; and all the auto-generated files are in the directory structure of "obj".

For example: let's say you use C# and XAML, and you create with XAML just three classes: Window classes "WindowA" and "WindowB", and also a UserControl class "ControlC". Then, in auto-generated code, you will be able to locate the files "WindowA.g.cs", "WindowB.g.cs" and "ControlC.g.cs". Look at them.

They are the files written in your development language (C#, VB.NET, C++/CLI, F# and the like). Open then and see what they do. This way, you can learn what happens. Usually, the code is more complicated than the code you would really need in your code behind, but for you, it's only important to get the idea: what FCL classes and what members to use to achieve what you want.

—SA


这篇关于如何在有效的方法中将xaml转换为代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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