Skip to main content
 首页 » 编程设计

c#之在 iOS 渲染器中是否有必要检查一个对象是否是我认为的对象(在本例中是 UITabBarController

2024年02月25日75pander-it

我有一个 iOS 渲染器,我想尝试了解一下这段代码。具体在这一点上:

    try 
    { 
        var tabbarController = (UITabBarController)this.ViewController; 
 
        if (null != tabbarController) 
        { 
            Xamarin.Forms.Application.Current.PropertyChanged += Current_PropertyChanged;  
            tabbarController.ViewControllerSelected += OnTabbarControllerItemSelected; 
            UpdateTheme(); 
        } 
    } 
    catch (Exception exception) 
    { 
        Console.WriteLine(exception); 
    } 

谁能告诉我进行所有这些检查有什么好处。例如。为什么这需要在“try”中,为什么我需要检查“if (null != tabbarController)”。我完全赞成使代码安全,但这些似乎太多了。我是正确的还是应该把这些支票留在里面?

作为引用,这里是完整的渲染器代码:

public class TabbedPageRenderer : TabbedRenderer 
{ 
    private MainPage _page; 
 
    protected override void OnElementChanged(VisualElementChangedEventArgs e) 
    { 
        base.OnElementChanged(e); 
 
        if (e.NewElement != null) 
        { 
            _page = (MainPage)e.NewElement; 
        } 
        else 
        { 
            _page = (MainPage)e.OldElement; 
        } 
 
        if (e.OldElement != null) 
        { 
            Xamarin.Forms.Application.Current.PropertyChanged -= Current_PropertyChanged; 
            return; 
        } 
 
        try 
        { 
            var tabbarController = (UITabBarController)this.ViewController; 
 
            if (null != tabbarController) 
            { 
                Xamarin.Forms.Application.Current.PropertyChanged += Current_PropertyChanged;  
                tabbarController.ViewControllerSelected += OnTabbarControllerItemSelected; 
                UpdateTheme(); 
            } 
        } 
        catch (Exception exception) 
        { 
            Console.WriteLine(exception); 
        } 
    } 
 
 
    private void OnTabbarControllerItemSelected(object sender, UITabBarSelectionEventArgs eventArgs) 
    { 
        if (!(_page.CurrentPage is Japanese.CardsTabPage) && App.quizRunning == true) 
        { 
            _page.CurrentPage = App.navCardsTabPage; 
        } 
    } 
 
    void Current_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
    { 
        if (e.PropertyName == "DarkTheme") 
            UpdateTheme(); 
    } 
 
    void UpdateTheme() 
    { 
        var isDarkTheme = (Xamarin.Forms.Application.Current as App).DarkTheme; 
 
        if (isDarkTheme) 
            TabBar.SelectedImageTintColor = UIColor.White; 
        else 
            TabBar.SelectedImageTintColor = UIColor.Red; 
 
    } 
 
} 

请您参考如下方法:

Can anyone tell me what would be the benefit of doing all these checks?

我会尽量提供帮助。

Why would this need to be inside a "try"

嗯,Try-catch block 通常用于程序员认为最有可能抛出某些或其他异常的区域,如果不处理可能会导致崩溃(您不希望您的应用程序现在崩溃你呢)。

why would I need to do the check "if (null != tabbarController)"

这个非常简单,实际上在您的代码生命周期中可能存在 PageRenderer 的 ViewController 属性可能为 null 的点,在这种情况下,如果您的代码到达 tabbarController.ViewControllerSelected += OnTabbarControllerItemSelected; 你认为会发生什么?

我说你的应用程序 BOOM(如果你忽略了 try catch。)。当然,从 C# 6 开始,您可以只执行 tabbarController?.ViewControllerSelected += OnTabbarControllerItemSelected; 但最后,重点是您将在此处执行不需要的代码行.

更新 我太匆忙了,甚至没有注意到代码行是一个赋值,因此你不能做 tabbarController?.ViewControllerSelected += OnTabbarControllerItemSelected;自“?”不能出现在作业的左侧。感谢 @apineda 指出这一点。

I am all for making code safe but these seem too much. Am I correct or should I leave these checks in?

我建议您保留这些检查,我个人认为其中没有任何不需要的代码。

注意:这些是我的个人观点,其他人可能会有所不同。

祝您好运,如有疑问,请随时回复。