我仍在学习 Xamarin Forms 和 C#。所以在我的 MainPage.xaml 中我有这个:
<StackLayout>
<Button Text="bttn1" Clicked="Button_Clicked"/>
<Button Text="bttn2" Clicked="Button_Clicked_1"/>
<ContentView x:Name="DisplayCustomContentView">
</ContentView>
</StackLayout>
和两个 ContentView:
View 1:
public class View1 : ContentView
{
public View1 ()
{
Content = new StackLayout {
Children = {
new Label { Text = "View 1" },
new Entry {Placeholder = "entry1 View 1"},
new Entry {Placeholder = "entry2 View 1"}
}
};
}
}
View 2:
public class View2 : ContentView
{
public View2 ()
{
Content = new StackLayout {
Children = {
new Label { Text = "View 2" },
new Entry {Placeholder = "entry2 View 1"},
new Entry {Placeholder = "entry2 View 2"}
}
};
}
}
所以我想在单击按钮时在 View 之间切换。我试过这个:
private void Button_Clicked(object sender, EventArgs e)
{
DisplayCustomContentView = new View1();
}
我如何从 Entry 字段中获取值?
我很确定我的做法不对。我可以使用我能得到的所有帮助!
最佳答案
回答你的问题:
And how can i get the values from the Entry fields?
有简单的答案,也有不太简单的答案。
由于您是在 C#(而不是 XAML)中构建 View1 和 View2,因此简单的答案是存储对 Entry 对象的引用:
public class View1 : ContentView
{
public Entry Entry1 { get; private set; }
public Entry Entry2 { get; private set; }
public View1 ()
{
Entry1 = new Entry { Placeholder = "entry1 View 1" };
Entry2 = new Entry { Placeholder = "entry1 View 2" };
Content = new StackLayout {
Children = {
new Label { Text = "View 1" },
Entry1,
Entry2
}
};
}
}
然后,在某些按钮回调或命令中(或每当您的程序获得控制权时),您可以查看 Entry1.Text 和 Entry2.Text 属性以查看用户输入的内容。
现在介绍不太简单的方法,MVVM 和模型绑定(bind)。虽然此代码涉及更多,但它是编写 Xamarin.Forms 应用程序的一种非常流行的方式,因为它使你能够更好地将 View 代码与其余逻辑分开。这有助于分离应用程序中的关注点,以提高可测试性、可维护性等。您可以使用 Xamarin 的内置功能来做到这一点,但很多人喜欢使用各种 MVVM 包来进一步支持 MVVM 原则。
要使用 View1(仅使用内置的 Xamarin.Forms 功能)进行简单说明,您还可以创建一个 View1ViewModel 类,如下所示:
public class View1ViewModel
{
public string Entry1 { get; set; }
public string Entry2 { get; set; }
}
public class View1 : ContentView
{
public View1ViewModel ViewModel { get; private set; }
public View1 ()
{
ViewModel = BindingContext = new View1ViewModel();
var entry1 = new Entry { Placeholder = "entry1 View 1" };
var entry2 = new Entry { Placeholder = "entry1 View 2" };
entry1.SetBinding(Entry.TextProperty, "Entry1");
entry2.SetBinding(Entry.TextProperty, "Entry2");
Content = new StackLayout {
Children = {
new Label { Text = "View 1" },
entry1,
entry2
}
};
}
}
在此模型中,其他代码可以查看 View 模型,而不是直接查看底层 UI 元素(Entry 对象)的属性,因为当条目的值更改时,这些属性会自动更新。
虽然此示例代码确实随着模型绑定(bind)的添加而变大,但在使用 XAML 时往往会更简洁一些。
当然,还有 MVVM 的其他方面(INotifyPropertyChanged 等),我鼓励您了解更多信息,但这超出了原始问题的范围。
关于c# - ContentView Xamarin 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51673026/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
我在事件管理员编辑页面中有嵌套资源,但我只想允许管理员编辑现有资源的内容,而不是添加新的嵌套资源。我的代码看起来像这样:formdo|f|f.inputsdof.input:authorf.input:contentf.has_many:commentsdo|comment_form|comment_form.input:contentcomment_form.input:_destroy,as::boolean,required:false,label:'Remove'endendf.actionsend但它在输入下添加了“添加新评论”按钮。我怎样才能禁用它,并只为主窗体保留f.ac
我目前正在尝试将ERB布局转换为HAML。这是我不断收到的错误:index.html.haml:18:syntaxerror,unexpected')'));}\n#{_hamlout.format_...这是HAML页面:.row-fluid.span6%h2TodoList.span6%h2{:style=>"text-align:right;"}document.write(today)%hr.divider.row-fluid.span6%h2.small_headNewTask=render:partial=>'layouts/form_errors',:locals=>{:
我如何做Ruby方法"Flatten"RubyMethod在C#中。此方法将锯齿状数组展平为一维数组。例如:s=[1,2,3]#=>[1,2,3]t=[4,5,6,[7,8]]#=>[4,5,6,[7,8]]a=[s,t,9,10]#=>[[1,2,3],[4,5,6,[7,8]],9,10]a.flatten#=>[1,2,3,4,5,6,7,8,9,10 最佳答案 递归解决方案:IEnumerableFlatten(IEnumerablearray){foreach(variteminarray){if(itemisIEnume
我最近从C#转向了Ruby,我发现自己无法制作可折叠的标记代码区域。我只是想到做这种事情应该没问题:classExamplebegin#agroupofmethodsdefmethod1..enddefmethod2..endenddefmethod3..endend...但是这样做真的可以吗?method1和method2最终与method3是同一种东西吗?还是有一些我还没有见过的用于执行此操作的Ruby惯用语? 最佳答案 正如其他人所说,这不会改变方法定义。但是,如果要标记方法组,为什么不使用Ruby语义来标记它们呢?您可以使用
什么是Linq聚合方法的ruby等价物。它的工作原理是这样的varfactorial=new[]{1,2,3,4,5}.Aggregate((acc,i)=>acc*i);每次将数组序列中的值传递给lambda时,变量acc都会累积。 最佳答案 这在数学以及几乎所有编程语言中通常称为折叠。它是更普遍的变形概念的一个实例。Ruby从Smalltalk中继承了这个特性的名称,它被称为inject:into:(像aCollectioninject:aStartValueinto:aBlock一样使用。)所以,在Ruby中,它称为inj