xamarin.ios – Monotouch对话框:样式元素

我正在使用Dialog并希望为我的所有细胞设置样式.我有一个背景图像,在示例中我可以看到如何使用StyledStringElement来使用该图像.

但是,在实际使用中,某些部分使用其他元素.例如,一个部分中的最后一个元素是RootElement – 但它没有要设置的BackgroundUri属性.布尔元素也是如此.

我发现了这个问题 – What’s the best way to customise all monotouch.dialog TableViewCells to the same style (Background, etc..)?这是一年半的类似问题.提到的UIAppearance样式确实存在于tablecells中,但不适用于MTDialog. krtrego对这个In monotouch.dialog can RootElement be easily styled?问题的回答声称可以完成这项工作,但是当我实施它时没有发生任何样式.

现在有没有改进的方法呢?实现我自己的“风格”版本的这些其他控件类型将是一个很大的努力,并看着styledstringelement这超出了我目前的技能水平.

这是我想要实现的一个例子(‘tags’单元格下方的阴影,但该元素实际上是一个RootElement,下面有一组无线电选项).删除默认的灰线等很容易,但是在每个部分的底部单元格上放置一个微妙的阴影是我无法解决的问题.

非常感谢!

PS.使用正常的MTDialog屏幕,单元格背景和边框被移除,每个部分下面都有一个微妙的白色阴影/线条.如果我能够重新说明我将会走很远的路,我想成为…

最佳答案 对元素进行子类化将允许您通过重写GetCell方法来设置它,但这变得相当繁琐.我遇到的最好的解决方案是通过子类化来创建自定义DialogViewController,并使用您想要的单元格的每个场景所需的图像(顶部,中间,底部,使用您自己的SizingSource和GetCell()方法覆盖CreateSizingSource方法.单独).它的一些代码和我的例子不会处理不均匀的行,但它是我见过的唯一不会修改MT.D源代码的解决方案.

以下是您在DialogViewController子类中重写的内容:

public override Source CreateSizingSource(bool unevenRows)
{
     return new CustomSource(unevenRows);
}

然后你将创建一个自定义源类:

public class CustomSource : Source
{
    public CustomSource(DialogViewController parent) : base (parent)
    {

    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var theCell = base.GetCell(tableView, indexPath);

        if (RowsInSection(tableView, indexPath.Section) == 1) //use one with top and bottom rounded
        {
            theCell.BackgroundView = new UIImageView(Theme.CellBackgroundFull);
            theCell.SelectedBackgroundView = new UIImageView(Theme.CellBackgroundFullActive); 

        } else if (indexPath.Row == 0) //top only
        {
            theCell.BackgroundView = new UIImageView(Theme.CellBackgroundTop);
            theCell.SelectedBackgroundView = new UIImageView(Theme.CellBackgroundTopActive); 

        } else if (indexPath.Row+1 == RowsInSection(tableView, indexPath.Section)) // bottom only
        {
            theCell.BackgroundView = new UIImageView(Theme.CellBackgroundBottom);
            theCell.SelectedBackgroundView = new UIImageView(Theme.CellBackgroundBottomActive); 
        } else //anything in the middle
        {
            theCell.BackgroundView = new UIImageView(Theme.CellBackgroundMiddle);
            theCell.SelectedBackgroundView = new UIImageView(Theme.CellBackgroundMiddleActive); 
        }
        return theCell;
    }
}

Theme只是一个返回UIImages的静态类,类似于Xamarin的示例Field Service应用程序.所以这里我总共制作了8张图片. 4表示元素的顶部,中间,底部和单独.每个都有不同的圆角,看起来是正确的.然后是一个“突出显示”的版本,当它触及时.

这里最大的缺点是你必须为你需要的每个不同风格的控制器执行此操作.如果你可以修改MT.D源代码,你可以得到一个不同的解决方案,允许你在Section部分控制它:http://fastchicken.co.nz/2012/05/20/earnest-debrief-visual-styles-in-ios-apps-uiappearence-custom-sections-in-monotouch-dialog/

它具有相同的效果,但您只需要为每个不同的样式子类化Section,这使得在一个Root中包含多个样式更容易.对此更改提出了拉取请求,但Miguel更喜欢第一个解决方案,见此处:https://github.com/migueldeicaza/MonoTouch.Dialog/pull/180

点赞