NotSupportedException when converting contentView

Viewed 29

MainPage.XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="POP.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:Ctrl="clr-namespace:POP.Controls"
    xmlns:Local="clr-namespace:KLAB.Localizations.Words;assembly=Localizations"
    xmlns:igGrid="clr-namespace:Infragistics.XamarinForms.Controls.Grids;assembly=Infragistics.XF.DataGrid">

    <ContentView>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="60" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="60" />
            </Grid.RowDefinitions>
            
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <ContentView x:Name="xTopLayout" Grid.Row="0" Grid.Column="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="PowderBlue">
                <Label  Text="상시 측정 위치" HorizontalOptions="Center"/>
                <!--<Ctrl:CtrlButton Clicked="CtrlButton_Clicked"/>-->
            </ContentView>


            <StackLayout x:Name="xPageView" Orientation="Horizontal" HorizontalOptions="FillAndExpand" Grid.Row="1" Grid.Column="0">
                
            </StackLayout>

            <ContentView x:Name="xBotLayout"  Grid.Row="2" Grid.Column="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="YellowGreen">
                <Label  Text="기타 버튼 위치" HorizontalOptions="Center"/>
            </ContentView>
        
        </Grid>
    </ContentView>
    
</ContentPage>

MainPage.cs

namespace POP
{
    public partial class MainPage : ContentPage, INotifyPropertyChanged
    {
        DisplayInfo m_MainDisplayInfo = DeviceDisplay.MainDisplayInfo;


        public MainPage()
        {
            InitializeComponent();
            InitMesureType();
        }


        private void InitMesureType()
        {
            xPageView.Children.Clear();
            xPageView.Children.Add(new MainView());
        }

        private void CtrlButton_Clicked(object sender, EventArgs e)
        {
            xPageView.Children.Clear();
            xPageView.Children.Add(new Spectrum());
        }
    }
}

MainView.XAML

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="POP.Component.MainView"
             xmlns:Ctrl="clr-namespace:POP.Controls"
             xmlns:Local="clr-namespace:KLAB.Localizations.Words;assembly=Localizations"
             xmlns:igGrid="clr-namespace:Infragistics.XamarinForms.Controls.Grids;assembly=Infragistics.XF.DataGrid">


    <StackLayout x:Name="m_ctrlView">
        <Ctrl:CtrlScrollView x:Name="xScroll"   Orientation="Horizontal" >
            <Grid x:Name="xMainContents" Margin="20,60,20,60" HorizontalOptions="CenterAndExpand" ColumnSpacing="20">

            </Grid>
        </Ctrl:CtrlScrollView>
    </StackLayout>
    
</ContentView>


MainView.cs

namespace POP.Component
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainView : ContentView
    {
        DisplayInfo m_MainDisplayInfo = DeviceDisplay.MainDisplayInfo;

        public MainView()
        {
            InitializeComponent();
            InitMainContents();
        }

        private void InitMainContents()
        {
            int nIndex = 0;
            foreach (MainContents contents in Enum.GetValues(typeof(MainContents)))
            {
                CtrlButton button = new CtrlButton
                {
                    WidthRequest = m_MainDisplayInfo.Width / 4,
                    HeightRequest = m_MainDisplayInfo.Height / 2,
                    Text = contents.ToString(),
                    ClassId = contents.ToString(),
                };
                button.Pressed += BtnMainContents;

                xMainContents.Children.Add(button, nIndex++, 0);
            }
        }

        private void BtnMainContents(object sender, EventArgs e)
        {
            var btn = (CtrlButton)sender;
            MainContents eType = (MainContents)Enum.Parse(typeof(MainContents), btn.ClassId);

            Console.WriteLine("BtnMainContents: " + eType.ToString());

            switch (eType)
            {
                case MainContents.Photometric:
                    {
                        break;
                    }
                case MainContents.Spectrum:
                    {
                        xMainContents.Children.Clear();

                        m_ctrlView.Children.Clear();
                        m_ctrlView.Children.Add(new Spectrum());
                        break;
                    }
                case MainContents.Kinetics:
                    {
                        break;
                    }
                case MainContents.Quantitation:
                    {
                        break;
                    }
                case MainContents.UserMethod:
                    {
                        break;
                    }
                case MainContents.Hach:
                    {
                        break;
                    }
                case MainContents.Merck:
                    {
                        break;
                    }
            }
        }
    }
}

When the button is clicked, the contents of the layout are cleared and the contents to be displayed are added, which occurs here.

What is the cause?

The final direction I want to take is to change the screen in the center when a specific event is received with the top and bottom fixed.

I haven't had much experience with Xamarin. Thank you in advance.

0 Answers
Related