how to fix Exception thrown: 'System.Runtime.I

2019-09-04 11:55发布

can help me fix this error? when I build my syntax no error, tp when I execute the error appeared, I have been using all means start to run VS 2015 deng run as administrator until exeption setting but nothing changes. any idea?

This is the XAML:

<Page
    x:Class="berhasil.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:berhasil"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:converter="using:berhasil"
    mc:Ignorable="d">

    <Page.Resources>
        <converter:Converter x:Key="Converter" />
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Text="Input Value: " VerticalAlignment="Center"/>
        <TextBox x:Name="textbox" 
                 Grid.Row="0" 
                 Grid.Column="1" 
                 Margin="0,10,0.333,0" 
                 Width="154" 
                 VerticalAlignment="Center"
                 InputScope="Number"/>
        <StackPanel Grid.Column="2"
                    Grid.Row="0"
                    VerticalAlignment="Center"
                    Margin="20,10,0,10"
                    Orientation="Horizontal">
            <ComboBox x:Name="combobox" 
                      ItemsSource="{x:Bind ComboBoxOptions}" 
                      SelectedItem="{x:Bind SelectedComboBoxOption, Mode=TwoWay, Converter={StaticResource ComboBoxItemConvert}}" 
                      SelectedValuePath="ComboBoxOption" 
                      DisplayMemberPath="ComboBoxOption">
            </ComboBox>
        </StackPanel>
        <Button Grid.Row="1" x:Name="button" 
                Content="Conversion" 
                HorizontalAlignment="Center" Margin="78,0,0.333,0" 
                Width="154" Grid.ColumnSpan="2"
                Click="button_Click"/>
        <TextBlock Grid.Row="2" Text="Celcius: " VerticalAlignment="Center"/>
        <TextBox x:Name="textbox1" 
                 Grid.Row="2" 
                 Margin="78,10,-23.333,0" 
                 Grid.ColumnSpan="3" 
                 VerticalAlignment="Center"
                 InputScope="Number" 
                 IsReadOnly="True"/>
        <TextBlock Grid.Row="3" Text="Reamur: " VerticalAlignment="Center"/>
        <TextBox x:Name="textbox2"
                 Grid.Row="3" 
                 Margin="78,10,-23.333,0" 
                 Grid.ColumnSpan="3" 
                 VerticalAlignment="Center"
                 InputScope="Number"
                 IsReadOnly="True"/>
        <TextBlock Grid.Row="4" Text="Fahrenheit: " VerticalAlignment="Center"/>
        <TextBox x:Name="textbox3"
                 Grid.Row="4" 
                 Margin="78,10,-23.333,0" 
                 Grid.ColumnSpan="3" 
                 VerticalAlignment="Center"
                 InputScope="Number"
                 IsReadOnly="True"/>
        <TextBlock Grid.Row="5" Text="Kelvin: " VerticalAlignment="Center"/>
        <TextBox x:Name="textbox4"
                 Grid.Row="5" 
                 Margin="78,10,-23.333,0" 
                 Grid.ColumnSpan="3" 
                 VerticalAlignment="Center"
                 InputScope="Number"
                 IsReadOnly="True"/>
    </Grid>
</Page>

And this is the code behind:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Xml.Serialization;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace berhasil
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page, INotifyPropertyChanged
    {
        Double awal, celcius, reamur, fahrenheit, kelvin;
        private ObservableCollection<ComboBoxItem> ComboBoxOptions;
        public int ComboBoxId { get; private set; }
        ApplicationDataContainer roamingSettings = null;


        public MainPage()
        {
            this.InitializeComponent();
            ComboBoxOptions = new ObservableCollection<ComboBoxItem>();
            ComboBoxOptionsManager.GetComboBoxList(ComboBoxOptions);
            roamingSettings = ApplicationData.Current.RoamingSettings;

            var value = (string)roamingSettings.Values["ComboBoxSelection"];
            if (value != null)
            {
                var deserialized = Deserialize<ComboBoxItem>(value);
                // using ComboBoxOption as the primary key field of your object
                SelectedComboBoxOption = ComboBoxOptions.SingleOrDefault(c =>
                            c.ComboBoxOption == deserialized.ComboBoxOption);
            }
            else
            {
                SelectedComboBoxOption = ComboBoxOptions[0];
            }
        }

        public class ComboBoxItem
        {
            public int ComboBoxId { get; set; }
            public string ComboBoxOption { get; set; }
        }

        public class ComboBoxOptionsManager
        {
            public static void GetComboBoxList(ObservableCollection<ComboBoxItem> ComboBoxItems)
            {
                var allItems = getComboBoxItems();
                ComboBoxItems.Clear();
                allItems.ForEach(p => ComboBoxItems.Add(p));
            }

            private static List<ComboBoxItem> getComboBoxItems()
            {
                var items = new List<ComboBoxItem>();

                items.Add(new ComboBoxItem() { ComboBoxId = 1, ComboBoxOption = "Celcius"});
                items.Add(new ComboBoxItem() { ComboBoxId = 2, ComboBoxOption = "Reamur" });
                items.Add(new ComboBoxItem() { ComboBoxId = 3, ComboBoxOption = "Fahrenheit" });
                items.Add(new ComboBoxItem() { ComboBoxId = 4, ComboBoxOption = "Kelvin" });

                return items;
            }
        }



        private ComboBoxItem _SelectedComboBoxOption;
        public ComboBoxItem SelectedComboBoxOption
        {
            get
            {
                return _SelectedComboBoxOption;
            }
            set
            {
                if (_SelectedComboBoxOption != value)
                {
                    _SelectedComboBoxOption = value;
                    roamingSettings.Values["ComboBoxSelection"] = Serialize(value);
                    RaisePropertyChanged("SelectedComboBoxOption");
                }
            }
        }

        public static string Serialize(object obj)
        {
            using (var sw = new StringWriter())
            {
                var serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(sw, obj);
                return sw.ToString();
            }
        }

        public static T Deserialize<T>(string xml)
        {
            using (var sw = new StringReader(xml))
            {
                var serializer = new XmlSerializer(typeof(T));
                return (T)serializer.Deserialize(sw);
            }
        }

        void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
        }
        public event PropertyChangedEventHandler PropertyChanged;



        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (Equals(ComboBoxId = 1))
            {
                awal = Convert.ToDouble(textbox.Text);
                celcius = awal;
                textbox1.Text = celcius.ToString();
                reamur = 0.8 * awal;
                textbox2.Text = reamur.ToString();
                fahrenheit = (1.8 * awal) + 32;
                textbox3.Text = fahrenheit.ToString();
                kelvin = awal + 273;
                textbox4.Text = kelvin.ToString();
            }
            else if (Equals(ComboBoxId = 2))
            {
                awal = Convert.ToDouble(textbox.Text);
                celcius = 1.25 * awal;
                textbox1.Text = celcius.ToString();
                reamur = awal;
                textbox2.Text = reamur.ToString();
                fahrenheit = (2.25 * awal) + 32;
                textbox3.Text = fahrenheit.ToString();
                kelvin = celcius + 273;
                textbox4.Text = kelvin.ToString();
            }
            else if (Equals(ComboBoxId = 3))
            {
                awal = Convert.ToDouble(textbox.Text);
                celcius = 0.55555 * (awal - 32);
                textbox1.Text = celcius.ToString();
                reamur = 0.44444 * (awal - 32);
                textbox2.Text = reamur.ToString();
                fahrenheit = awal;
                textbox3.Text = fahrenheit.ToString();
                kelvin = celcius + 273;
                textbox4.Text = kelvin.ToString();
            }
            else if (Equals(ComboBoxId = 4))
            {
                awal = Convert.ToDouble(textbox.Text);
                celcius = awal - 273;
                textbox1.Text = celcius.ToString();
                reamur = 0.8 * (awal - 273);
                textbox2.Text = reamur.ToString();
                fahrenheit = (1.8 * (awal - 273)) + 32;
                textbox3.Text = fahrenheit.ToString();
                kelvin = awal;
                textbox4.Text = kelvin.ToString();
            }
        }
    }

    public class Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value as MainPage.ComboBoxItem;
        }
    }
}

1条回答
倾城 Initia
2楼-- · 2019-09-04 12:19

Combobox may be causing the issue. moreover you are using ComboBoxItemConvert converter which is missing in this page at least!

查看更多
登录 后发表回答