【问题标题】:IMarkupExtension - Check property type during compilation time (xamarin.forms)IMarkupExtension - 在编译期间检查属性类型 (xamarin.forms)
【发布时间】:2018-11-16 18:34:08
【问题描述】:

如果 IMarkupExtension 中的给定参数与我期望的类型不兼容,我想在编译期间抛出异常。 我能达到这个效果吗?

下面我放了我的实验,但我不知道在哪里以及如何检查我在“TODO”中写的内容

代码(我标记了待办事项)

using System;
using Xamarin.Forms.Xaml;

namespace MySample
{
    public class SampleClass : IMarkupExtension
    {
        public IParameter Parameter { get; set; }
        public object ProvideValue(IServiceProvider serviceProvider)
        {
            return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
        }
    }

    public interface IParameter
    {
        string GetData();
    }
    public class SampleData1 : IParameter
    {
        public string GetData()
        {
            return "Data1";
        }
    }
    public class SampleData2 : IParameter
    {
        public string GetData()
        {
            return "Data2";
        }
    }
}

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mysample="clr-namespace:MySample"
             x:Class="MySample.SamplePage">
    <ContentPage.Resources>
        <mysample:SampleData2 x:Key="SampleData2" />
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout>
            <Label>
                <Label.Text>
                    <mysample:SampleClass Parameter="{StaticResource SampleData2}" />
                </Label.Text>
            </Label>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

请注意,参数是 SampleData2 类型,但如果不是 SampleData1 类型,我想抛出异常

资源

<mysample:SampleData2 x:Key="SampleData2" />

资源使用情况

Parameter="{StaticResource SampleData2}"

检查(不一定在这个地方,但肯定在编译期间)

public object ProvideValue(IServiceProvider serviceProvider)
{
    return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
}

【问题讨论】:

    标签: xamarin.forms compilation-time


    【解决方案1】:

    我认为不可能在编译时抛出异常。编译器无法检测到逻辑错误,因此只有在程序执行时才会检测到。

    编译时错误:

    如果我们不遵循任何编程语言的正确语法和语义,那么编译器会抛出编译时错误。

    例如:

    1.缺少分号

    2.关键字大写

    3.varaiable 不反抗等

    运行时错误:

    程序处于运行状态时会产生运行时错误。它们通常被称为例外。

    例如:

    1.除以零

    2.内存不足

    3.取消引用空指针等

    当此函数被触发并且Parameter 不是SampleData1 类型时,您可以使用下面的代码来throw a Exception

     public object ProvideValue()
            {
    
                if (Parameter is SampleData1)
                {
    
                    return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
                }
                else if (Parameter is SampleData2)
                {   
    
                    throw new Exception("Parameter must be of type SampleData1");                
                }
                return "error";
            }
    

    【讨论】:

    • 是的,你可能是对的。但是,我自欺欺人地认为有人会给出一个令人惊讶的解决方案。感谢您的评论和帮助。
    猜你喜欢
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 2012-06-10
    相关资源
    最近更新 更多