运行后台进程,使用Xamarin.forms中的计时器更改另一页的背景图像
嗨,我的团队 我正在尝试在我的应用程序中运行后台进程。此后台进程应每15秒更新一次应用程序中某个页面上的背景图像。到目前为止,我试图在App OnStart()方法中创建一个计时器,并在BeginInvokeOnMainThread()方法中更新页面的背景图像,但没有成功。有人能帮我吗 我的代码-运行后台进程,使用Xamarin.forms中的计时器更改另一页的背景图像,xamarin.forms,timer,background-process,Xamarin.forms,Timer,Background Process,嗨,我的团队 我正在尝试在我的应用程序中运行后台进程。此后台进程应每15秒更新一次应用程序中某个页面上的背景图像。到目前为止,我试图在App OnStart()方法中创建一个计时器,并在BeginInvokeOnMainThread()方法中更新页面的背景图像,但没有成功。有人能帮我吗 我的代码- { private static Stopwatch stopWatch = new Stopwatch(); private const int defau
{
private static Stopwatch stopWatch = new Stopwatch();
private const int defaultTimespan = 20;
private readonly HomePage homePage;
public App()
{
InitializeComponent();
try
{
MainPage = new MainPage();
homePage = new HomePage();
}
catch(Exception ex)
{
string str = ex.Message;
}
}
protected override void OnStart()
{
if (!stopWatch.IsRunning)
{
stopWatch.Start();
}
Device.StartTimer(new TimeSpan(0, 0, 10), () =>
{
// Logic for logging out if the device is inactive for a period of time.
if (stopWatch.IsRunning && stopWatch.Elapsed.Seconds >= defaultTimespan)
{
//prepare to perform your data pull here as we have hit the 1 minute mark
// Perform your long running operations here.
Device.BeginInvokeOnMainThread(() =>
{
// If you need to do anything with your UI, you need to wrap it in this.
// homePage.BackgroundImageSource = "goldengate.jpg";
homePage.ChangeBackgroundImage();
});
stopWatch.Restart();
}
// Always return true as to keep our device timer running.
return true;
});
}
protected override void OnSleep()
{
//stopWatch.Reset();
}
protected override void OnResume()
{
//stopWatch.Start();
}
//void ChangeHomePageImage()
//{
// Navigation.PushAsync(new HomePage(appBackground));
// Navigation.RemovePage(this);
//}
}
MainPage -
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:Excercise.Views"
x:Class="Excercise.MainPage" IsPresented="False">
<MasterDetailPage.Master>
<local:MenuPage x:Name="menuPage"></local:MenuPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<local:HomePage x:Name="homePage"></local:HomePage>
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
HomePage -
public partial class HomePage : ContentPage
{
private SQLiteAsyncConnection _connection;
public HomePage()
{
InitializeComponent();
// BindingContext = new HomePageViewModel();
_connection = DependencyService.Get<ISQLiteDb>().GetConnection();
loadData("");
}
public HomePage(string BackgroundimgPath)
{
InitializeComponent();
// BindingContext = new HomePageViewModel();
_connection = DependencyService.Get<ISQLiteDb>().GetConnection();
loadData(BackgroundimgPath);
}
public HomePage(string City, string LocationKey, string StateID)
{
InitializeComponent();
_connection = DependencyService.Get<ISQLiteDb>().GetConnection();
// BindingContext = new HomePageViewModel();
try
{
// Method Calls
}
catch (Exception)
{
DisplayAlert("Error", "There was an error loading this page.", "OK");
}
}
protected override void OnAppearing()
{
this.Title = App.AppTitle;
this.firstStacklayout.Margin = new Thickness(0, (Application.Current.MainPage.Height * 0.25), 0, 0);
base.OnAppearing();
}
{
专用静态秒表秒表=新秒表();
private const int defaultTimespan=20;
私人只读网页;
公共应用程序()
{
初始化组件();
尝试
{
主页=新主页();
主页=新主页();
}
捕获(例外情况除外)
{
string str=ex.Message;
}
}
受保护的覆盖void OnStart()
{
如果(!秒表正在运行)
{
秒表。开始();
}
Device.StartTimer(新的时间跨度(0,0,10),()=>
{
//如果设备在一段时间内处于非活动状态,则用于注销的逻辑。
if(stopWatch.IsRunning&&stopWatch.appeased.Seconds>=defaultTimespan)
{
//准备在这里执行数据提取,因为我们已经达到了1分钟的目标
//在此处执行长时间运行的操作。
Device.beginInvokeMainThread(()=>
{
//如果你需要对你的UI做任何事情,你需要把它包装在这里。
//homePage.BackgroundImageSource=“goldengate.jpg”;
homePage.ChangeBackgroundImage();
});
stopWatch.Restart();
}
//始终返回true以保持设备计时器运行。
返回true;
});
}
受保护的覆盖void OnSleep()
{
//秒表复位();
}
受保护的覆盖void OnResume()
{
//秒表。开始();
}
//void ChangeHomePageImage()
//{
//PushAsync(新主页(appBackground));
//导航.RemovePage(此);
//}
}
主页-
主页-
公共部分类主页:ContentPage
{
专用SQLiteAsyncConnection\u连接;
公共网页()
{
初始化组件();
//BindingContext=新的HomePageViewModel();
_connection=DependencyService.Get().GetConnection();
加载数据(“”);
}
公共主页(字符串背景imgpath)
{
初始化组件();
//BindingContext=新的HomePageViewModel();
_connection=DependencyService.Get().GetConnection();
loadData(BackgroundimgPath);
}
公共主页(字符串City、字符串LocationKey、字符串StateID)
{
初始化组件();
_connection=DependencyService.Get().GetConnection();
//BindingContext=新的HomePageViewModel();
尝试
{
//方法调用
}
捕获(例外)
{
DisplayAlert(“错误”,“加载此页面时出错”,“确定”);
}
}
出现时受保护的覆盖无效()
{
this.Title=App.AppTitle;
this.firstStacklayout.Margin=新厚度(0,(Application.Current.MainPage.Height*0.25),0,0);
base.OnAppearing();
}
您正在创建主页
的实例并尝试更新它,但它与您的主细节中显示的实例不同
试试这样的
var md = (MasterDetailPage)MainPage;
var nav = (NavigationPage)md.DetailPage;
var home = (HomePage)nav.CurrentPage;
home.ChangeBackgroundImage();
或者,您可以使用
MessagingCenter
向主页
发送一条消息,要求它显示您的代码。我已经用我到目前为止的代码编辑了我的问题。我使用了此资源-您创建了一个主页实例,但不做任何事情来在屏幕上显示它是一个单页应用程序。主页将始终显示,我只想更改背景图像,而不刷新整个UI。你在哪里这样做?在你发布的代码中,你MainPage=newmainpage()
我收到一个错误-System.InvalidCastException:“指定的强制转换无效。”您的主页被包装在导航页面中,因此您需要解释我不理解的原因。您能详细解释一下吗?或者我该如何做?我是Xamarin的新手,正在尝试找出如何使这项工作正常进行。