简体   繁体   English

编辑和使用Xamarin.Forms源代码

[英]Editing and using the Xamarin.Forms source code

I was wondering if it was possible to edit the Xamarin.Forms source code and then use the edited one like you normally would in your xamarin.forms project. 我想知道是否可以编辑Xamarin.Forms源代码 ,然后像在xamarin.forms项目中通常那样使用编辑后的源代码

Basically, my goal would be to change the PhoneMasterDetailRenderer in order to change the width value of the Master page. 基本上,我的目标是更改PhoneMasterDetailRenderer以便更改“母版”页面的宽度值。 (It is a percentage of the screen, which is 0.8 , and so by changing that it should adjust the size of the master?) (这是屏幕的百分比,为0.8 ,因此通过更改它应该调整母版的大小?)

Here is the section of code I wish to change: 这是我要更改的代码部分:

    void LayoutChildren(bool animated)
    {
        var frame = Element.Bounds.ToRectangleF();
        var masterFrame = frame;
        masterFrame.Width = (int)(Math.Min(masterFrame.Width, masterFrame.Height) * 0.8);

        ...
    }

The issue of not being able to change the width of the master has been a problem for a very long time, and hopefully this may lead to a solution. 很长时间以来,无法更改母版的宽度一直是一个问题,希望可以解决该问题。

Thanks, Daniel. 谢谢,丹尼尔。

I don't recommend you to edit the source code. 我不建议您编辑源代码。 But we can also create our own MasterDetailPage's Renderer. 但是我们也可以创建自己的MasterDetailPage的Renderer。 It may be a little difficult, let's do this step by step. 可能有些困难,让我们逐步进行此操作。

Firstly, define a BindableProperty in our own MasterDetailPage class like: 首先,在我们自己的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);
    }
}

Secondly, try to create our own renderer instead of using the form's default renderer. 其次,尝试创建自己的渲染器,而不是使用表单的默认渲染器。 I post my source code here about my own renderer. 在此处发布有关自己的渲染器的源代码。 In this class I use widthRatio changing the master's width. 在此类中,我使用widthRatio更改母版的宽度。 This property can be set in: 可以在以下位置设置此属性:

void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
{
    ...
    else if(e.PropertyName == "WidthRatio")
    {
        widthRatio = ((MyMasterDetailPage)Element).WidthRatio;
    }
}

At last, create the custom renderer inheriting the renderer above like: 最后,创建继承上述渲染器的自定义渲染器,如下所示:

[assembly: ExportRenderer(typeof(MyMasterDetailPage), typeof(MyMasterDetailPageRenderer))]
namespace MasterDetailDemo.iOS
{
    public class MyMasterDetailPageRenderer : MyPhoneMasterDetailRenderer
    {
    }
}

You can set the property WidthRatio 's value in forms's MasterDetailPage to change the width now. 您可以在表单的MasterDetailPage设置属性WidthRatio的值,以立即更改宽度。 You can run my demo to test it. 您可以运行我的演示进行测试。

Besides if you want to do this on Android, please refer to this thread . 此外,如果您要在Android上执行此操作,请参考此线程

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

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