Xamarin.forms 如何添加单独的项目和类库的初始化
我使用的是Xamarin表单和一个为sikutuion提供一些解决方案和额外设置的提供者。我想在完全分离的项目中添加该设置,这样如果我使用不同的提供者,就不会有问题了。因此,我添加了类库并开始实现该解决方案。但是现在我有点不确定如何处理这个问题 我添加了和引用,现在刚刚测试连接,我得到了系统空引用 我的xaml,我现在正试图显示类库中的字符串Xamarin.forms 如何添加单独的项目和类库的初始化,xamarin.forms,Xamarin.forms,我使用的是Xamarin表单和一个为sikutuion提供一些解决方案和额外设置的提供者。我想在完全分离的项目中添加该设置,这样如果我使用不同的提供者,就不会有问题了。因此,我添加了类库并开始实现该解决方案。但是现在我有点不确定如何处理这个问题 我添加了和引用,现在刚刚测试连接,我得到了系统空引用 我的xaml,我现在正试图显示类库中的字符串 <ContentPage.Content> <Grid Grid.Row ="
<ContentPage.Content>
<Grid Grid.Row ="1" RowSpacing="0" >
<Label Grid.Row="0" Text="{Binding TestString}" HorizontalOptions="Start" Padding="10,0,0,0" VerticalOptions="Center" />
</Grid>
</ContentPage.Content>
Back of my page
public partial class TestService : ContentPage
{
private readonly Regula.CustomSettings res;
public TestService(Regula.CustomSettings res)
{
InitializeComponent();
res = new Regula.CustomSettings();
BindingContext = new Test(res);
}
}
图书馆呢
public class CustomSettings
{
public string TestString = "test";
public CustomSettings()
{
TestString = "Test";
}
}
请你告诉我如何从图书馆取我的字符串好吗?或者如何用谷歌搜索这些东西?您可以修改自定义设置,如下所示:
public class CustomSettings
{
public string TestString { set; get; }
}
public class Test: BaseViewModel
{
public CustomSettings customSettings { set; get; }
public Test(CustomSettings cusSettings)
{
this.customSettings = cusSettings;
}
public string Name
{
get { return customSettings.TestString; }
set
{
if (customSettings.TestString == value)
return;
customSettings.TestString = value;
OnPropertyChanged();
}
}
}
然后修改测试视图模型,如下所示:
public class CustomSettings
{
public string TestString { set; get; }
}
public class Test: BaseViewModel
{
public CustomSettings customSettings { set; get; }
public Test(CustomSettings cusSettings)
{
this.customSettings = cusSettings;
}
public string Name
{
get { return customSettings.TestString; }
set
{
if (customSettings.TestString == value)
return;
customSettings.TestString = value;
OnPropertyChanged();
}
}
}
然后在TestServicecontentpage中:
public partial class TestService : ContentPage
{
public TestService(Regula.CustomSettings res)
{
InitializeComponent();
CustomSettings customSettings = new CustomSettings() { TestString = "TestData" };
Test testModel = new Test(customSettings);
this.BindingContext = testModel;
}
}
其效果是:
注意:如果要将MVVM与
ListView
一起使用,可以参考。为什么要将CustomSettings
作为参数传递给构造函数,然后将其设置为新实例?这没有任何意义,您在两个构造函数中都在这样做。您能告诉我正确的方法吗This.res=res代码>对不起,自定义设置是项目外部的类库吗?这是共享项目、iOS、android和另一个项目自定义设置,仅基于您发送的示例。我认为它没有回答问题。@VanessaKensington Okey,您可以共享使用共享项目或示例项目链接的代码吗?我会检查,然后可以修改我的答案。如果可以,我会上传它并让你知道