我试图按照标题所说的去做,我已经搜索了,什么也没找到。
希望任何人都可以提供帮助,这是问题所在
我有一个不需要注册的应用程序,只需登录即可,数据来自网络服务。这是登录方法。
public async Task<string> LoginAsync(string user, string pass, string company)
{
RssUser userInfos = new RssUser{ user = user, pass = pass, company = "PROJECTO_GPS" };
RootObject ro = new RootObject();
ro.rssUser = userInfos;
var jsonObj = JsonConvert.SerializeObject(ro);
using (HttpClient client = new HttpClient())
{
StringContent content = new StringContent(jsonObj.ToString(), Encoding.UTF8, "application/json");
var request = new HttpRequestMessage()
{
RequestUri = new Uri("http://evolus.ddns.net/Q4Evolution/php/phpGps/BOLoginGps.php"),
Method = HttpMethod.Post,
Content = content
};
var response = await client.SendAsync(request);
string dataResult = response.Content.ReadAsStringAsync().Result;
Debug.WriteLine(dataResult);
return dataResult;
}
}
这是Rss类:
public class RssUser
{
private AppService _log = new AppService();
public string user { get; set; }
public string pass { get; set; }
public string company { get; set; }
public ICommand LoginCommmand
{
get
{
return new Command(async () =>
{
await _log.LoginAsync(user, pass, company);
});
}
}
}
public class RootObject
{
public RssUser rssUser { get; set; }
}
这很好,我的问题是我如何获取此登录名的用户名以绑定到登录后将出现在页面中的工具栏上。
以下是Xaml的代码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:AppQ4evo.ViewModels"
x:Class="AppQ4evo.Menu"
NavigationPage.HasBackButton="False">
<ContentPage.BindingContext>
<viewModels:RssUser/>
</ContentPage.BindingContext>
<ContentPage.ToolbarItems>
<ToolbarItem Name="icon" Order="Primary" Icon="srw_circular.png" Priority="0" />
<ToolbarItem Icon="baseline_account_circle_black_18dp.png" Order="Primary" Text="{Binding user}" Priority="0" />
<ToolbarItem Name="LogOut" Order="Primary" Text="Log Out" Priority="0" Clicked="LogOut_Clicked"/>
</ContentPage.ToolbarItems>...
所以问题是Text="{Binding user}"
,我见过很多不同的绑定方式,但是我真的不能使它们中的任何一种起作用。希望有人能给我一些启示。
谢谢。
答案 0 :(得分:0)
UPDATE
类未实现INotifyPropertyChange,因此永远不会告诉视图RssUser
已更改。在实现接口后,您的类的外观示例如下:
user
最后,您需要更新绑定以匹配公共用户属性:class RssUser: INotifyPropertyChanged
{
private AppService _log = new AppService();
string user;
string pass;
string company;
public event PropertyChangedEventHandler PropertyChanged;
public ICommand LoginCommmand
{
get
{
return new Command(async () =>
{
await _log.LoginAsync(user, pass, company);
});
}
}
public string User
{
set
{
if (user != value)
{
user = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(User)))
}
}
get
{
return user;
}
}
// You would also want to similar for pass and company.
}
请注意,可以将属性更改后的逻辑放在基类中并从中继承您的VM。