编辑和使用 Xamarin.Forms 源代码 [英] Editing and using the Xamarin.Forms source code

查看:27
本文介绍了编辑和使用 Xamarin.Forms 源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以编辑 Xamarin.Forms 源代码,然后像往常一样在 xamarin.forms 项目中使用编辑过的文件.

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.

基本上,我的目标是改变 PhoneMasterDetailRenderer 以更改母版页的宽度值.(它是屏幕的百分比,是0.8,所以通过改变它应该调整母版的大小?)

Basically, my goal would be to change the PhoneMasterDetailRenderer in order to change the width value of the Master page. (It is a percentage of the screen, which is 0.8, and so by changing that it should adjust the size of the master?)

这是我希望更改的代码部分:

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.

谢谢,丹尼尔.

推荐答案

我不建议您编辑源代码.但是我们也可以创建我们自己的 MasterDetailPage 的 Renderer.可能有点难,我们一步一步来吧.

I don't recommend you to edit the source code. But we can also create our own MasterDetailPage's Renderer. It may be a little difficult, let's do this step by step.

首先,在我们自己的MasterDetailPage类中定义一个BindableProperty,如:

Firstly, define a BindableProperty in our own MasterDetailPage class like:

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 来改变主人的宽度.这个属性可以设置在:

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. 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
    {
    }
}

您现在可以在表单的MasterDetailPage 中设置属性WidthRatio 的值来更改宽度.你可以运行我的演示来测试它.

You can set the property WidthRatio's value in forms's MasterDetailPage to change the width now. You can run my demo to test it.

此外,如果您想在 Android 上执行此操作,请参阅此主题.

Besides if you want to do this on Android, please refer to this thread.

这篇关于编辑和使用 Xamarin.Forms 源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆