编辑和使用Xamarin.Forms源代码
我想知道是否可以编辑Xamarin.Forms源代码 ,然后像你通常在xamarin.forms项目中那样使用编辑过的源代码 。
基本上,我的目标是更改PhoneMasterDetailRenderer以更改Master页面的宽度值。 (它是屏幕的百分比,为0.8 ,因此通过更改它应该调整主屏幕的大小?)
以下是我希望更改的代码部分:
void LayoutChildren(bool animated) { var frame = Element.Bounds.ToRectangleF(); var masterFrame = frame; masterFrame.Width = (int)(Math.Min(masterFrame.Width, masterFrame.Height) * 0.8); ... }
无法改变主机宽度的问题在很长一段时间内都是一个问题,希望这可能会导致解决方案。
谢谢,丹尼尔。
我不建议您编辑源代码。 但我们也可以创建自己的MasterDetailPage的渲染器。 这可能有点困难,让我们一步一步来做。
首先,在我们自己的MasterDetailPage
类中定义一个BindableProperty
,如:
public readonly static BindableProperty WidthRatioProperty = BindableProperty.Create("WidthRatio", typeof(float), typeof(MyMasterDetailPage), (float)0.2); public float WidthRatio { get { return (float)GetValue(WidthRatioProperty); } set { SetValue(WidthRatioProperty, value); } }
其次,尝试创建我们自己的渲染器,而不是使用表单的默认渲染器。 我在这里发布了关于我自己的渲染器的源代码。 在本课程中,我使用widthRatio
更改主人的宽度。 此属性可以设置为:
void HandlePropertyChanged(object sender, PropertyChangedEventArgs e) { ... else if(e.PropertyName == "WidthRatio") { widthRatio = ((MyMasterDetailPage)Element).WidthRatio; } }
最后,创建inheritance上面渲染器的自定义渲染器,如:
[assembly: ExportRenderer(typeof(MyMasterDetailPage), typeof(MyMasterDetailPageRenderer))] namespace MasterDetailDemo.iOS { public class MyMasterDetailPageRenderer : MyPhoneMasterDetailRenderer { } }
您可以在窗体的MasterDetailPage
设置属性WidthRatio
的值,以立即更改宽度。 您可以运行我的演示来测试它。
此外,如果您想在Android上执行此操作,请参阅此主题 。