[英]Editing and using the Xamarin.Forms source code
我想知道是否可以编辑Xamarin.Forms源代码 ,然后像在xamarin.forms项目中通常那样使用编辑后的源代码 。
基本上,我的目标是更改PhoneMasterDetailRenderer以便更改“母版”页面的宽度值。 (这是屏幕的百分比,为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的Renderer。 可能有些困难,让我们逐步进行此操作。
首先,在我们自己的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;
}
}
最后,创建继承上述渲染器的自定义渲染器,如下所示:
[assembly: ExportRenderer(typeof(MyMasterDetailPage), typeof(MyMasterDetailPageRenderer))]
namespace MasterDetailDemo.iOS
{
public class MyMasterDetailPageRenderer : MyPhoneMasterDetailRenderer
{
}
}
您可以在表单的MasterDetailPage
设置属性WidthRatio
的值,以立即更改宽度。 您可以运行我的演示进行测试。
此外,如果您要在Android上执行此操作,请参考此线程 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.