Home > Net >  Tried running my xamarin.forms ios app on Mac M1. Crashed when I opened any page that had TableView
Tried running my xamarin.forms ios app on Mac M1. Crashed when I opened any page that had TableView

Time:12-04

I was curious to see if the Xamarin.Forms app I've been working on (XF 4.8) would run on Mac with M1, now that TestFlight is available.

Almost flawless, except for at least two different pages I load that have a XF TableView. I suppose I could have a bug there, but it does work great in iOS and iPadOS.

Ironically, looking closer, I reminded myself I have a very complicated table view that DOES work. I wonder why?

There may be a hint: I finally looked very close at a crash report, and found a reference to a line of code in XF: D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\TableViewModelRenderer.cs:74

From GitHub, this appears to be (in context):

public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section)
{
    if (headerView is UITableViewHeaderFooterView header)
    {
        var sectionHeaderTextColor = View.Model.GetSectionTextColor((int)section);

        if (sectionHeaderTextColor != Color.Default)
        {
74>         header.TextLabel.TextColor = sectionHeaderTextColor.ToUIColor();
        }
    }
}

Checking, a difference between the TableView that works and the ones that fail: TableSection has both a "Title" and a "TextColor".

Has anyone else seen this? The only thing I see is that Apple lists "TextLabel" in UITableViewHeaderFooterView as deprecated. I don't think that should cause this, should it?

Bob

CodePudding user response:

I was curious to see if the Xamarin.Forms app I've been working on (XF 4.8) would run on Mac with M1, now that TestFlight is available.

Almost flawless, except for at least two different pages I load that have a XF TableView. I suppose I could have a bug there, but it does work great in iOS and iPadOS.

Do you mean it only works on iPhone/iPad (real device) but not work on Mac M1(simulate) ?

Have you tried it on a None-M1 mac ? What happened ?

I strongly recommend you to update XF to the latest version(5.xxx) to see if problem persists.


If problem still exists you can create custom renderer for TableView and override the logic in WillDisplayHeaderView method , because I see the textLabel is no longer available after iOS14, for change the label text color , we can use UILabel appearance , see enter image description here

Sample code
[assembly: ExportRenderer(typeof(TableView), typeof(MyTableViewRenderer))]
namespace FormsApp.iOS
{
    internal class MyTableViewRenderer : TableViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
                return;

            var tableView = Control as UITableView;
            var formsTableView = Element as TableView;

            MyModelRenderer render = new MyModelRenderer(formsTableView);
            tableView.WeakDelegate = render;

           
        }
    }

    public class MyModelRenderer : TableViewModelRenderer
    {
        public MyModelRenderer(TableView model) : base(model)
        {

        }

        public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section)
        {
            if (headerView is UITableViewHeaderFooterView header)
            {
                var sectionHeaderTextColor = View.Model.GetSectionTextColor((int)section);

                if (sectionHeaderTextColor != Color.Default)
                {
                    UILabel.AppearanceWhenContainedIn(typeof(UITableViewHeaderFooterView)).TextColor = sectionHeaderTextColor.ToUIColor();  //change this line
                }
            }
        }
    }
}

CodePudding user response:

Thanks to ColeX -- probably the right way to do it.

So, I was trying to run my app AS AN iOS APP on my Mac M1 (the real device) -- not simulate. It mostly worked, as I said.

BUT, I looked at my TableView cases, realized I did not need the text in the Section header, so got rid of it and the color -- NOW IT WORKS ON MAC! YEAH!

  • Related