System.Reflection.TargetInvocationException将设置页面实例化为MasterDetail菜单中的导航页面

特拉维斯·弗莱诺(Travis Fleenor)

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation尝试从MasterDetail菜单启动设置页面时出现错误从研究中我知道,这意味着无法实例化该页面。这是页面的xaml

我正在使用James Montenegro的xam.plugins.settings存储设置。很卡住了。

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                         x:Class="EIOBoardMobile.Views.SettingsPage"
                         Title="Settings">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Connection Information" />
            <StackLayout>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <StackLayout Orientation="Vertical" Grid.Row="0" Grid.Column="0"  >
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer 
                                            Tapped="OnServerTapped" 
                                                NumberOfTapsRequired="1" />
                        </StackLayout.GestureRecognizers>
                        <Label Text="Server name" />
                        <Label x:Name="Server" Text="{Binding Connection}" />
                    </StackLayout>
                    <Label Text="{Binding UserName}" Grid.Row="1" Grid.Column="0"/>
                    <Label Text="{Binding Password}" Grid.Row="2" Grid.Column="0"/>
                    <StackLayout Orientation="Vertical" Grid.Row="3" Grid.Column="0" >
                        <Label Text="Test Connection" />
                        <Label Text="Click to test your connection settings" />
                    </StackLayout>
                </Grid>
                <Label Text="Location" />
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="***" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Label Text="Location Tracking" Grid.Row="0" Grid.Column="0" />
                    <Switch IsToggled="{Binding LocationTracking}" Grid.Row="0" Grid.Column="1" />
                    <StackLayout Orientation="Vertical" Grid.Row="1" Grid.Column="0">
                        <Label Text="Location Caching" />
                        <Label Text="WARNING! Experimental! Stores location data while offline." />
                    </StackLayout>
                    <Switch IsToggled="{Binding LocationCaching}" Grid.Row="1" Grid.Column="1" />
                </Grid>
                <Label Text="Logging Options" />
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="***" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <StackLayout Orientation="Vertical" Grid.Row="0" Grid.Column="0">
                        <Label Text="Allow Logging" />
                        <Label Text="Collecting crash data" />
                    </StackLayout>
                    <Switch IsToggled="{Binding AllowLogging}" Grid.Row="0" Grid.Column="1" />
                    <StackLayout Orientation="Vertical" Grid.Row="1" Grid.Column="0">
                        <Label Text="Crash log email" />
                        <Label Text="Provide us an email to contact you regarding crash logs" />
                    </StackLayout>
                </Grid>
                <Label Text="Caching Options" />
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="***" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <StackLayout Orientation="Vertical" Grid.Row="0" Grid.Column="0">
                        <Label Text="Caching" />
                        <Label Text="CacheSize:" />
                    </StackLayout>
                    <Switch IsToggled="{Binding Caching}" Grid.Row="0" Grid.Column="1" />
                    <StackLayout Orientation="Vertical" Grid.Row="1" Grid.Column="0">
                        <Label Text="Delete Cache" />
                        <Label Text="Click to delete the application cache" />
                    </StackLayout>
                </Grid>
                <Label Text="Updates" />
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="***" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Label Text="Notify me of new updates" Grid.Row="0" Grid.Column="0" />
                    <Switch IsToggled="{Binding UpdateNotify}" Grid.Row="0" Grid.Column="1" />
                    <Label Text="Check for updates" Grid.Row="1" Grid.Column="0" />
                </Grid>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

这是背后的代码

using EIOBoardMobile.ViewModel;
using EIOBoardMobile.Views.popups;
using Rg.Plugins.Popup.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace EIOBoardMobile.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SettingsPage : ContentPage
    {
        public SettingsPage ()
        {
            InitializeComponent ();
            BindingContext = new SettingsPageViewModel();
        }

        public async void OnServerTapped(object sender, EventArgs e)
        {
            await Navigation.PushPopupAsync(new ConnectionPopupPage());
        }
    }
}

和ViewModel

using EIOBoardMobile.Helpers;
using EIOBoardMobile.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Input;

namespace EIOBoardMobile.ViewModel
{
    public class SettingsPageViewModel : INotifyPropertyChanged
    {
        public ICommand ItemSelectedCommand { get; private set; }

        public SettingsPageViewModel()
        {

        }

        public void OnCreate()
        {

        }

        public string Connection
        {
            get { return Settings.ConnectionAddress; }
            set { Settings.ConnectionAddress = value; }
        }

        public string UserName
        {
            get { return Settings.UserName; }
            set { Settings.UserName = value; }
        }

        public string Password
        {
            get { return Settings.Password; }
            set { Settings.Password = value; }
        }

        public bool LocationTracking
        {
            get { return Settings.LocationTracking; }
            set { Settings.LocationTracking = value; }
        }

        public bool LocationCaching
        {
            get { return Settings.LocationCaching; }
            set { Settings.LocationCaching = value; }
        }

        public bool AllowLogging
        {
            get { return Settings.AllowLogging; }
            set { Settings.AllowLogging = value; }
        }

        public string CrashLogEmail
        {
            get { return Settings.CrashLogEmail; }
            set { Settings.CrashLogEmail = value; }
        }

        public bool Caching
        {
            get { return Settings.Caching; }
            set { Settings.Caching = value; }
        }

        public bool UpdateNotify
        {
            get { return Settings.UpdateNotify; }
            set { Settings.UpdateNotify = value; }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(
            [CallerMemberName] string caller = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(caller));
            }
        }
    }
}

知道我在做什么错吗?IDE未显示任何错误。我尝试设置OnTapped2种或3种不同方式的操作。我不能在StackLayout里面一个Grid吗?任何帮助是极大的赞赏。

寿司宿醉

GridUnitType是一个Enum

  • 绝对:将“值”属性值解释为特定于设备的单位数。
  • 自动:忽略“值”属性值,并选择适合行或列子级的大小。
  • 星号:将“值”属性值解释为比例权重,在考虑“绝对”或“自动”的行和列之后进行布局。

因此,按照Xamarin的XAML规则,这将无效

<ColumnDefinition Width="***" />

这些是有效的:

<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="100" />

您的XAML应该是这样的:

<ContentPage.Content>
    <StackLayout>
        <Label Text="Connection Information" />
        <StackLayout>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <StackLayout Orientation="Vertical" Grid.Row="0" Grid.Column="0"  >
                    <StackLayout.GestureRecognizers>
                        <TapGestureRecognizer 
                                        Tapped="OnServerTapped" 
                                            NumberOfTapsRequired="1" />
                    </StackLayout.GestureRecognizers>
                    <Label Text="Server name" />
                    <Label x:Name="Server" Text="{Binding Connection}" />
                </StackLayout>
                <Label Text="{Binding UserName}" Grid.Row="1" Grid.Column="0"/>
                <Label Text="{Binding Password}" Grid.Row="2" Grid.Column="0"/>
                <StackLayout Orientation="Vertical" Grid.Row="3" Grid.Column="0" >
                    <Label Text="Test Connection" />
                    <Label Text="Click to test your connection settings" />
                </StackLayout>
            </Grid>
            <Label Text="Location" />
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Label Text="Location Tracking" Grid.Row="0" Grid.Column="0" />
                <Switch IsToggled="{Binding LocationTracking}" Grid.Row="0" Grid.Column="1" />
                <StackLayout Orientation="Vertical" Grid.Row="1" Grid.Column="0">
                    <Label Text="Location Caching" />
                    <Label Text="WARNING! Experimental! Stores location data while offline." />
                </StackLayout>
                <Switch IsToggled="{Binding LocationCaching}" Grid.Row="1" Grid.Column="1" />
            </Grid>
            <Label Text="Logging Options" />
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <StackLayout Orientation="Vertical" Grid.Row="0" Grid.Column="0">
                    <Label Text="Allow Logging" />
                    <Label Text="Collecting crash data" />
                </StackLayout>
                <Switch IsToggled="{Binding AllowLogging}" Grid.Row="0" Grid.Column="1" />
                <StackLayout Orientation="Vertical" Grid.Row="1" Grid.Column="0">
                    <Label Text="Crash log email" />
                    <Label Text="Provide us an email to contact you regarding crash logs" />
                </StackLayout>
            </Grid>
            <Label Text="Caching Options" />
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <StackLayout Orientation="Vertical" Grid.Row="0" Grid.Column="0">
                    <Label Text="Caching" />
                    <Label Text="CacheSize:" />
                </StackLayout>
                <Switch IsToggled="{Binding Caching}" Grid.Row="0" Grid.Column="1" />
                <StackLayout Orientation="Vertical" Grid.Row="1" Grid.Column="0">
                    <Label Text="Delete Cache" />
                    <Label Text="Click to delete the application cache" />
                </StackLayout>
            </Grid>
            <Label Text="Updates" />
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Label Text="Notify me of new updates" Grid.Row="0" Grid.Column="0" />
                <Switch IsToggled="{Binding UpdateNotify}" Grid.Row="0" Grid.Column="1" />
                <Label Text="Check for updates" Grid.Row="1" Grid.Column="0" />
            </Grid>
        </StackLayout>
    </StackLayout>        
</ContentPage.Content>

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

'System.Reflection.TargetInvocationException'(在MVVM中)

异常信息:System.Reflection.TargetInvocationException

未捕获System.Reflection.TargetInvocationException

System.Reflection.TargetInvocationException错误(C#)

wpf组合框中的System.Reflection.TargetInvocationException

System.Reflection.TargetInvocationException:调用的目标已抛出异常。发生

如何在初始化期间设置DataTemplate元素-'System.Reflection.TargetInvocationException'

XCODE中的Json.Net错误-System.Reflection.Emit

用system.reflection代替“ this”

什么是System.Reflection.Module?

System.Reflection.TargetException 错误

System.ArgumentException:在 System.Reflection.MonoPropert 中找不到“XXXXXXX”的获取方法

Application Insights失败,并在加载时出现System.Reflection.TargetInvocationException-WP 8.1

Xamarin Forms MessagingCenter:System.Reflection.TargetInvocationException:调用的目标引发了异常

当我运行MVC 4应用程序时出现System.Reflection.TargetInvocationException

Xamarin Forms System.Reflection.TargetInvocationException:“调用的目标已引发异常。”

System.Reflection.TargetInvocationException:调用的目标已引发异常。将SwipeView放入ListView时

EntityFramework.SqlServer.dll中发生类型为'System.Reflection.TargetInvocationException'的异常,但未在用户代码中处理

Reflection.TargetInvocationException

如何避免使用System.Reflection在ComboBox中显示Enum属性值

如何使用 System.Reflection.Emit 解决 Xamarin.iOS 中的 MTOUCH (MT2002) 错误

未处理的异常:Unity3d中的System.Reflection.ReflectionTypeLoadException

为什么 System.Reflection.Assembly 中的 GetName 在反编译时似乎抛出 NotImplementedException?

Reflection是C#中System名称空间内的嵌套名称空间吗?

System.Reflection.MissingRuntimeArtifactException-在发布模式下

System.Reflection.Emit-如果语句生成

PropertieInfo的GetValue引发TargetParameterCountException(System.Reflection)

system.reflection无法获取属性值

使用GetValue和System.Reflection