简体   繁体   English

如何在其控制器中投射 XAML 项目? [C#],[Xamarin.Forms]

[英]How do I cast an XAML item in its controller? [C#],[Xamarin.Forms]

I'm new to Xamarin and I'm facing a problem when it comes to use a xaml item (A button or Label for example) in the controller of this .xaml file.我是 Xamarin 的新手,在此 .xaml 文件的控制器中使用 xaml 项目(例如按钮或标签)时遇到问题。

I just dont know how to declare the item inside the controller, for example in java we use something like我只是不知道如何在控制器内声明项目,例如在 java 中我们使用类似的东西

Button btnSave = (Button) findViewByID(R.xmlFILE.btnSave);

then we can use the "btnSave" as an object inside the class controller, but when it comes to Xamarin.forms, I just cant seem to find how I do that, please help me.然后我们可以使用“btnSave”作为类控制器内的对象,但是当涉及到 Xamarin.forms 时,我似乎无法找到我是如何做到的,请帮助我。

in XAML, assign a name to the control在 XAML 中,为控件分配一个名称

<Button x:Name="myButton" ... />

then in your xaml.cs file, you just refer to it by name.然后在您的 xaml.cs 文件中,您只需按名称引用它。 You don't need to declare it or cast it你不需要声明它或投射它

myButton.Text = "blah";

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

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