我試圖在我的 Xamarin 應用程式中使用資料系結。實施后,就像許多說明所建議的那樣,它只是在加載應用程式時總是崩潰。沒有任何真正的錯誤資訊。
我想我做錯了什么,但經過數小時的搜索,我沒有找到任何線索來解決我的問題。
這是終端內的文本:
主頁.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodels="clr-namespace:TippAssist.ViewModels"
x:Class="TippAssist.MainPage">
<ContentPage.BindingContext>
<viewmodels:MainPageViewmodel/>
</ContentPage.BindingContext>
<StackLayout>
<Label Text="{Binding test}"></Label>
</StackLayout>
</ContentPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace TippAssist
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
}
}
主頁面視圖模型
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace TippAssist.ViewModels
{
public class MainPageViewmodel : BindableObject
{
public string test {
get => test;
set
{
if (value == test)
return;
test = value;
OnPropertyChanged();
}
}
public MainPageViewmodel()
{
this.test = "IT WORKS!";
}
}
}
如果有人能告訴我我犯的錯誤,我會非常高興。
非常感謝你:)
uj5u.com熱心網友回復:
您的應用程式崩潰了,因為您將值分配給相同的公共屬性。創建一個私有屬性“_test”并按如下方式使用它:
private string _test;
public string test
{
get => _test;
set
{
if (value == _test)
return;
_test = value;
OnPropertyChanged();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/478217.html
