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未显示任何错误。我尝试设置OnTapped
2种或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] 删除。
我来说两句