如何在Xamarin.Forms中引用外部类中的页面元素



我的Xamarin.Forms应用程序中有几个页面需要类似的功能,所以我决定创建一个外部实用程序类,我可以在所有这些页面中引用它,以减少冗余代码。

其中一页的代码是:

public partial class Incontrol_page : TabbedPage
{
public Incontrol_page()
{
Boolean initialrun = true;
Alert_Generator generator = new Alert_Generator();
InitializeComponent();
Task.Run(() => 
{
while (true)
{
//code not relevant to the question removed here
Device.BeginInvokeOnMainThread(() => 
{
List<Frame> offline_alerts = generator.GenAlerts("incontrol_offline", active_offline_events);
List<Frame> bandwidth_alerts = generator.GenAlerts("incontrol_bandwidth", active_bandwidth_events);
offline_stack.Children.Clear();
bandwidth_stack.Children.Clear();
foreach(Frame frame in offline_alerts)
{
offline_stack.Children.Add(frame);
}
foreach(Frame frame in bandwidth_alerts)
{
bandwidth_stack.Children.Add(frame);
}
});
Thread.Sleep(10000);
}
});
}

}

从本质上讲,该方法从API中提取数据,并创建一个帧列表,根据提取的数据类型略有不同。这部分工作得很好,但是我在框架本身遇到了功能问题。以下是用于制作帧的代码示例:

public List<Frame> GenAlerts(string type, List<String> active_events)
{
using (WebClient wc = new WebClient())
{
//irrelevant code removed
foreach (var obj in list)
{
//irrelevant code removed
Frame alert_frame = new Frame
{
//irrelevant code removed
Content = new StackLayout
{
//irrelevant code removed
Children =
{
Orientation=Xamarin.Forms.StackOrientation.Horizontal,
Children =
{
GetChildren(obj,color,wc,type)[0],
GetChildren(obj,color,wc,type)[1]
}
}
}
};
return_list.Add(alert_frame);
}
return return_list;
}
}
public List<View> GetChildren(Alert obj, Color color, WebClient wc, string type)
{
//creates info button with popup for the event readout
Button info = new Button()
{
//attributes here
};
info.Clicked += async (sender, args) => await DisplayAlert("Event Info", obj.Change, "Okay");
//creates delete button that deletes the parent alert object from the stack layout and sends the command to the api to remove it from the SQL table
Button delete = new Button()
{
//attributes here
};
delete.Clicked += async (sender, args) => await Remove(obj, wc, type);
List<View> alert_frames = new List<View>();
alert_frames.Add(info);
alert_frames.Add(delete);
return alert_frames;
}
async Task Remove(Alert obj, WebClient wc, string type)
{
View frame = new Frame();
foreach (Frame element in alert_stack.Children)
{
if (element.StyleId == obj.EventKey)
{
frame = element;
}
}
alert_stack.Children.Remove(frame);
//sends the remove command for the event's id to the api
var jsonstr = wc.DownloadString("http://172.30.211.33:5000/remove?db="+type+"&id=" + obj.EventKey);
}

对我来说,最大的问题出现在Remove(Alert obj,WebClient wc,string type(任务中。在以前的实现中,当所有这些都直接在每个页面的代码后面,而不是在可重用的方法中时,我可以简单地获得父框架的引用,并在按下按钮时删除所需的元素,但从没有直接连接到页面的外部方法来看,这并不容易。我是否可以将对alert_stack的引用作为GenAlerts((的参数传递?

同样,我不确定如何使DisplayAlert((函数调用从外部方法中实际工作。有没有一种方法可以让我引用调用该函数的父页面,让它在屏幕上实际显示警报?

我是否可以将对alert_stack的引用作为GenAlerts((的参数?

是。你试过了吗?它应该像一样简单

List<Frame> offline_alerts = generator.GenAlerts(offline_stack, "incontrol_offline", active_offline_events);

public List<Frame> GenAlerts(StackLayout parent, string type, List<String> active_events)

有没有一种方法可以让我获得一些对父页面的引用

是。您可以传入对当前页面的显式引用,也可以执行此

App.Current.MainPage.DisplayAlert(...);

最新更新