C# 更改xaml控件';s值从单独的cs/命名空间在多个页面中显示
首先,我很抱歉我的解释不好,因为我对c#和移动应用程序开发相当陌生。 我必须在我的IOS和Android Xamarin跨平台项目中添加页面。假设a.xaml和b.xaml。这两个页面都有两个不同的名称空间(例如第一个和第二个)。假设我在a.xaml中有一个标签(lblA),在b.xaml中有一个按钮(btnB)。我想做的是在单击btnB时更新lblA.text。我尝试了x:Name=“btnB”,但无法从第二个命名空间访问btnB。有办法吗 背景:现在让我解释一下问题的背景。我正在尝试在我的多页跨平台项目中实现Signaler。其中我有一个单独的信号器类,在MessageReceived事件中,我希望在不同页面中更新相关标签/网格。因此,我需要访问我所有页面中的控件。当我尝试执行Debug.Writeline(“从信号器接收的消息”)时,它工作得非常好,但当我尝试更改First.lblA.text时,它不工作,也不会引发任何异常 因此,任何建议都将受到高度赞赏 谢谢 信号员:C# 更改xaml控件';s值从单独的cs/命名空间在多个页面中显示,c#,xamarin,cross-platform,signalr.client,visual-studio-2017,C#,Xamarin,Cross Platform,Signalr.client,Visual Studio 2017,首先,我很抱歉我的解释不好,因为我对c#和移动应用程序开发相当陌生。 我必须在我的IOS和Android Xamarin跨平台项目中添加页面。假设a.xaml和b.xaml。这两个页面都有两个不同的名称空间(例如第一个和第二个)。假设我在a.xaml中有一个标签(lblA),在b.xaml中有一个按钮(btnB)。我想做的是在单击btnB时更新lblA.text。我尝试了x:Name=“btnB”,但无法从第二个命名空间访问btnB。有办法吗 背景:现在让我解释一下问题的背景。我正在尝试在我的多
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
using System.Collections.Generic;
using Xamarin.Forms;
namespace SigClient
{
public class Client
{
public string _user = "";
public string _prefix = "";
public string _url = "";
public int _port = 80;
public string User
{
get
{
return _user;
}
set
{
_user = value;
}
}
public string Prefix
{
get
{
return _prefix;
}
set
{
_prefix = value;
}
}
public string HubUrl
{
get
{
return _url;
}
set
{
_url = value;
}
}
public int Port
{
get
{
return _port;
}
set
{
_port = value;
}
}
private HubConnection _connection;
private IHubProxy _proxy;
public void Clients()
{
var querystringData = new Dictionary<string, string>();
querystringData.Add("userName", "{\"userName\":\"" + _prefix + "_" + "driver" + _user + "\"}");
_connection = new HubConnection(_url + ":" + _port.ToString(), querystringData);
_proxy = _connection.CreateHubProxy("MyHub");
}
private App1.MainPage page;
public Label lbl;
public async Task Connect()
{
try
{
page = new App1.MainPage();
_proxy.On<string, string>("addNewMessageToPage", (name, msg) =>
//-> I want to update testlbl.text=msg
//page.exLbl.Text = msg //doesnt do anything
//Debug.WriteLine(msg) //This line works fine
deb("", msg)
);
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
}
await _connection.Start();
}
public async Task deb(string msg, string msg1)
{
//page.exLbl.Text = msg1
page.DoStuff(msg1);
lbl = page.exLbl;
lbl.Text = msg1;
Debug.WriteLine(msg + "-" + msg1);
}
public Task Send(string message)
{
Debug.WriteLine("SignalR message sent");
return _proxy.Invoke("SendToOperators", "eb5_operator123456", "NewJobSaved", "system");
}
}
}
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
using System.Collections.Generic;
using Xamarin.Forms;
namespace SigClient
{
public class Client
{
public string _user = "";
public string _prefix = "";
public string _url = "";
public int _port = 80;
public string User
{
get
{
return _user;
}
set
{
_user = value;
}
}
public string Prefix
{
get
{
return _prefix;
}
set
{
_prefix = value;
}
}
public string HubUrl
{
get
{
return _url;
}
set
{
_url = value;
}
}
public int Port
{
get
{
return _port;
}
set
{
_port = value;
}
}
private HubConnection _connection;
private IHubProxy _proxy;
public void Clients(App1.MainPage page)
{
this.page=page;
var querystringData = new Dictionary<string, string>();
querystringData.Add("userName", "{\"userName\":\"" + _prefix + "_" + "driver" + _user + "\"}");
_connection = new HubConnection(_url + ":" + _port.ToString(), querystringData);
_proxy = _connection.CreateHubProxy("MyHub");
}
private App1.MainPage page;
//public Label lbl;
public async Task Connect()
{
try
{
_proxy.On<string, string>("addNewMessageToPage", (name, msg) =>
page.PublicLbl.Text = msg
//Debug.WriteLine(msg) //This line works fine
deb("", msg)
);
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
}
await _connection.Start();
}
public async Task deb(string msg, string msg1)
{
//you don't need both of these lines
page.PublicLbl.Text = msg1
page.DoStuff(msg1);
Debug.WriteLine(msg + "-" + msg1);
}
public Task Send(string message)
{
Debug.WriteLine("SignalR message sent");
return _proxy.Invoke("SendToOperators", "eb5_operator123456", "NewJobSaved", "system");
}
}
}
再次,请原谅我的新手编码风格。请随时问我任何问题
谢谢。默认情况下,.Net中的控件受到保护,因此可以公开访问。您始终可以公开该控件 例如,在类A中,可以定义返回受保护控件的只读属性
public class A {
public Label LblA { get {return lblA;}}
}
在B中,您可以访问LblA,因为它现在是公共的
public class B {
private A aRef; /*a reference to an actual instance of A*/
public void setALabel() {
aRef.LblA.text="Some Text";
}
}
在您的代码中,客户端正在连接时创建主页的新实例。要使代码正常工作,客户端类中的页面变量需要保存对已创建页面的引用
信号员:
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
using System.Collections.Generic;
using Xamarin.Forms;
namespace SigClient
{
public class Client
{
public string _user = "";
public string _prefix = "";
public string _url = "";
public int _port = 80;
public string User
{
get
{
return _user;
}
set
{
_user = value;
}
}
public string Prefix
{
get
{
return _prefix;
}
set
{
_prefix = value;
}
}
public string HubUrl
{
get
{
return _url;
}
set
{
_url = value;
}
}
public int Port
{
get
{
return _port;
}
set
{
_port = value;
}
}
private HubConnection _connection;
private IHubProxy _proxy;
public void Clients()
{
var querystringData = new Dictionary<string, string>();
querystringData.Add("userName", "{\"userName\":\"" + _prefix + "_" + "driver" + _user + "\"}");
_connection = new HubConnection(_url + ":" + _port.ToString(), querystringData);
_proxy = _connection.CreateHubProxy("MyHub");
}
private App1.MainPage page;
public Label lbl;
public async Task Connect()
{
try
{
page = new App1.MainPage();
_proxy.On<string, string>("addNewMessageToPage", (name, msg) =>
//-> I want to update testlbl.text=msg
//page.exLbl.Text = msg //doesnt do anything
//Debug.WriteLine(msg) //This line works fine
deb("", msg)
);
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
}
await _connection.Start();
}
public async Task deb(string msg, string msg1)
{
//page.exLbl.Text = msg1
page.DoStuff(msg1);
lbl = page.exLbl;
lbl.Text = msg1;
Debug.WriteLine(msg + "-" + msg1);
}
public Task Send(string message)
{
Debug.WriteLine("SignalR message sent");
return _proxy.Invoke("SendToOperators", "eb5_operator123456", "NewJobSaved", "system");
}
}
}
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
using System.Collections.Generic;
using Xamarin.Forms;
namespace SigClient
{
public class Client
{
public string _user = "";
public string _prefix = "";
public string _url = "";
public int _port = 80;
public string User
{
get
{
return _user;
}
set
{
_user = value;
}
}
public string Prefix
{
get
{
return _prefix;
}
set
{
_prefix = value;
}
}
public string HubUrl
{
get
{
return _url;
}
set
{
_url = value;
}
}
public int Port
{
get
{
return _port;
}
set
{
_port = value;
}
}
private HubConnection _connection;
private IHubProxy _proxy;
public void Clients(App1.MainPage page)
{
this.page=page;
var querystringData = new Dictionary<string, string>();
querystringData.Add("userName", "{\"userName\":\"" + _prefix + "_" + "driver" + _user + "\"}");
_connection = new HubConnection(_url + ":" + _port.ToString(), querystringData);
_proxy = _connection.CreateHubProxy("MyHub");
}
private App1.MainPage page;
//public Label lbl;
public async Task Connect()
{
try
{
_proxy.On<string, string>("addNewMessageToPage", (name, msg) =>
page.PublicLbl.Text = msg
//Debug.WriteLine(msg) //This line works fine
deb("", msg)
);
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
}
await _connection.Start();
}
public async Task deb(string msg, string msg1)
{
//you don't need both of these lines
page.PublicLbl.Text = msg1
page.DoStuff(msg1);
Debug.WriteLine(msg + "-" + msg1);
}
public Task Send(string message)
{
Debug.WriteLine("SignalR message sent");
return _proxy.Invoke("SendToOperators", "eb5_operator123456", "NewJobSaved", "system");
}
}
}
刚才试过了,但没用。主页中的更改:公共标签exLbl{get{return testLbl;}}}和Sinalr.cs中的更改:private App1.MainPage;自有品牌lbl;公共异步任务连接(){try{page=new App1.MainPage();_proxy.On(“addNewMessageToPage”(名称,msg)=>page.exLbl.Text=msg//Debug.WriteLine(msg)//deb(“,msg));}。感谢您的快速响应。页面=new App1.MainPage();正在创建MainPage的新实例。您应该引用客户端已经看到的现有实例,而不是创建新实例。例如,在主页构造函数中添加
cclient.page=this;
并在客户端中更改`private App1.MainPage;`topublic App1.MainPage;
或将主页作为参数传递请转到客户端构造函数并在客户端构造函数中设置私有变量或完全修改您的代码以显示如何实现它。好极了。我回家后会检查并向您汇报。非常感谢您的努力,伙计。非常感谢。您当时只能看到1页,不应该更新页面由于您不知道这些事件的状态,因此当前无法看到这些事件。说您正在尝试这样做似乎不是一个好方法。为什么不从Signal客户端公开公共事件,这些事件可以稍后从您的应用程序页面中使用,然后在那里更新状态?或者您甚至可以使用MessagingCenter。嘿,谢谢您的建议手势。只要读几篇关于它的文章,它看起来很有希望。我会在接下来的几天休息,等我回来的时候会给它一个机会。希望它能起作用。谢谢
<?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:local="clr-namespace:App1"
x:Class="App1.MainPage"
x:FieldModifier="public">
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<Label Text="Welcome to Xamarin Forms!"
VerticalOptions="Center"
HorizontalOptions="Center" ></Label>
<Label x:Name="testLbl" VerticalOptions="Center" HorizontalOptions="Center"></Label>
<Label x:Name="clientLbl" VerticalOptions="Center" HorizontalOptions="Center"></Label>
<Button x:Name="testBtn" Text="Connect"></Button>
<Button x:Name="testSend" Text="Send Msg"></Button>
</StackLayout>
</ContentPage>
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Xamarin.Forms;
using SigClient;
using UIKit;
namespace App1
{
public partial class MainPage : ContentPage
{
public Label PublicLbl
{
get {return clientLbl;}
}
public Client cclient
public MainPage()
{
InitializeComponent();
cclient=new Client(this);
testLbl.Text = "I'm Loaded...";
cclient.User = "111";
cclient.Prefix = "xxx";
cclient.HubUrl = "http://255.255.255.255";
cclient.Port = 80;
testBtn.Clicked += new EventHandler(testBtn_Click);
testSend.Clicked += new EventHandler(testSend_Click);
}
async void testSend_Click(Object sender, EventArgs e)
{
try
{
await cclient.Send("test");
testLbl.Text = "Sent!!!";
}
catch (Exception ex)
{
testLbl.Text = ex.Message;
}
}
async void testBtn_Click(Object sender, EventArgs e)
{
try
{
cclient.Clients();
await cclient.Connect();
//this line will going to overwrite the client message in the original code
testLbl.Text = "Connected!!!";
}
catch(Exception ex)
{
testLbl.Text = ex.Message;
}
}
public void DoStuff(string x)
{
this.clientLbl.Text = x;
Debug.WriteLine(x + " dostuff"); //this line works fine
}
}
}