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

查看:239
本文介绍了编辑和使用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天全站免登陆