英文:
Recover published data in another page
问题
I have a page that allows you to publish something (which contains a name, a description, an image, etc.) and I am trying to retrieve what I publish on another page so that I can then manage it (modified, deleted, etc.) . But I can't really get this back.
When I launch it shows me exactly the same thing as what is on the page for publishing.
I also tried to publish, then after going back to the page, but it always displays the same page as the other.
Post page
New page (shows me the same thing)
FileResult file;
NewTheoryViewModel theoryViewModel;
public NewTheoryPage()
{
theoryViewModel = new NewTheoryViewModel();
//activitieViewModel.GetItemCategories().GetAwaiter().GetResult();
BindingContext = theoryViewModel;
InitializeComponent();
}
private async void Upload_Clicked(object sender, EventArgs e)
{
// shortcuts...
}
private async void Save_Clicked(object sender, EventArgs e)
{
TheoriesModel theory = new();
// ...shortcuts...
return;
}
var stream = ...shortcuts
string fileName = file.FileName;
theory.Image_id = imagesModel.Id > 0 ? imagesModel.Id : 1;
theory.Group_id = 2... shortcuts
var response = await Services.NewTheory.PostNewActivitie(theory);
if (response.IsSuccessStatusCode)
{
await DisplayAlert("Great", "The theory has been added", "Ok");
await Navigation.PopModalAsync();
}
}
// I tried adding this after var response = await Services.NewTheory.PostNewActivitie(theory);
if (response.IsSuccessStatusCode):
var testPage = new SettingsPage
{
BindingContext = theory
};
// and at the top: public TheoriesModel TheoryPublished { get; private set; }
In the page where I want to retrieve: GetPublishedData();
}
private void GetPublishedData()
{
var newTheoryPage = new NewTheoryPage();
// Attach an event handler to the Disappearing event of NewTheoryPage
newTheoryPage.Disappearing += NewTheoryPage_Disappearing;
Navigation.PushModalAsync(newTheoryPage);
}
private void NewTheoryPage_Disappearing(object sender, EventArgs e)
{
var newTheoryPage = (NewTheoryPage)sender;
// Check if data has been published
if (newTheoryPage.TheoryPublished != null)
{
// Use data published here
var publishedTheory = newTheoryPage.TheoryPublished;
// Do whatever you want with the data (display, delete, modify, etc.)
}
// Detach the event handler from the Disappearing event
newTheoryPage.Disappearing -= NewTheoryPage_Disappearing;
}
<!-- In the other page: -->
<Label Text="Published Theory" FontAttributes="Bold" />
<Label Text="{Binding PublishedTheory.Name}" />
<Label Text="{Binding PublishedTheory.Description}" />
// In the other page:
MessagingCenter.Subscribe<NewTheoryPage, TheoriesModel>(this, "TheoryPublished", OnTheoryPublished);
}
private void OnTheoryPublished(NewTheoryPage sender, TheoriesModel theory)
{
lblName.Text = theory.Name;
lblDescription.Text = theory.Description;
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<NewTheoryPage, TheoriesModel>(this, "TheoryPublished");
}
// In NewTheoryPage:
var response = await Services.NewTheory.PostNewActivitie(theory);
if (response.IsSuccessStatusCode)
{
MessagingCenter.Send(this, "TheoryPublished", theory);
await DisplayAlert("Great", "The theory has been added", "Ok");
await Navigation.PopModalAsync();
}
I put the code with global static:
public class AppSettings
{
public static string Parameter1 { get; set; }
public static string Parameter2 { get; set; }
}
In the other page:
private void OnTheoryPublished(NewTheoryPage sender, TheoriesModel theory)
{
AppSettings.Parameter1 = theory.Name;
System.Diagnostics.Debug.WriteLine("access the value of Parameter1 : " + AppSettings.Parameter1);
AppSettings.Parameter2 = theory.Description;
System.Diagnostics.Debug.WriteLine("access the value of Parameter2 : " + AppSettings.Parameter2);
lblName.Text = AppSettings.Parameter1;
lblDescription.Text = AppSettings.Parameter2;
}
英文:
I have a page that allows you to publish something (which contains a name, a description, an image, etc.) and I am trying to retrieve what I publish on another page so that I can then manage it (modified, deleted, etc.) . But I can't really get this back.
When I launch it shows me exactly the same thing as what is in the page for published
I also tried to publish then after going back to the page but it always displays the same page as the other
Post page
New page (shows me the same thing)
FileResult file;
NewTheoryViewModel theoryViewModel;
public NewTheoryPage()
{
theoryViewModel = new NewTheoryViewModel();
//activitieViewModel.GetItemCategories().GetAwaiter().GetResult();
BindingContext = theoryViewModel;
InitializeComponent();
}
private async void Upload_Clicked(object sender, EventArgs e)
{
shortcuts...
}
private async void Save_Clicked(object sender, EventArgs e)
{
TheoriesModel theory = new();
.....shortcuts
return;
}
var stream = ...shortcuts
string fileName = file.FileName;
theory.Image_id = imagesModel.Id > 0 ? imagesModel.Id : 1;
theory.Group_id = 2... shortcuts
var response = await Services.NewTheory.PostNewActivitie(theory);
if (response.IsSuccessStatusCode)
{
await DisplayAlert("Great", "The theory has been added", "Ok");
await Navigation.PopModalAsync();
}
}
}
if (response.IsSuccessStatusCode):
var testPage = new SettingsPage
{
BindingContext = theory
};
and at the top: public TheoriesModel TheoryPublished { get; private set; }
In the page where I want to retrieve: GetPublishedData();
}
private void GetPublishedData()
{
var newTheoryPage = new NewTheoryPage();
// Attach an event handler to the Disappearing event of NewTheoryPage
newTheoryPage.Disappearing += NewTheoryPage_Disappearing;
Navigation.PushModalAsync(newTheoryPage);
}
private void NewTheoryPage_Disappearing(object sender, EventArgs e)
{
var newTheoryPage = (NewTheoryPage)sender;
// Check if data has been published
if (newTheoryPage.TheoryPublished != null)
{
// Use data published here
var publishedTheory = newTheoryPage.TheoryPublished;
// Do whatever you want with the data (display, delete, modify, etc.)
}
// Detach the event handler from the Disappearing event
newTheoryPage.Disappearing -= NewTheoryPage_Disappearing;
}
}```
```<Label Text="Published Theory" FontAttributes="Bold" />
<Label Text="{Binding PublishedTheory.Name}" />
<Label Text="{Binding PublishedTheory.Description}" />```
In other page :
``` MessagingCenter.Subscribe<NewTheoryPage, TheoriesModel>(this, "TheoryPublished", OnTheoryPublished);
}
private void OnTheoryPublished(NewTheoryPage sender, TheoriesModel theory)
{
lblName.Text = theory.Name;
lblDescription.Text = theory.Description;
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<NewTheoryPage, TheoriesModel>(this, "TheoryPublished");
}
}
and in NewTheoryPage:
var response = await Services.NewTheory.PostNewActivitie(theory);
if (response.IsSuccessStatusCode)
{
MessagingCenter.Send(this, "TheoryPublished", theory);
await DisplayAlert("Great", "The theory has been added", "Ok");
await Navigation.PopModalAsync();
}
I put the code with global static
public class AppSettings
{
public static string Parameter1 { get; set; }
public static string Parameter2 { get; set; }
}
Other page :
{
AppSettings.Parameter1 = theory.Name;
System.Diagnostics.Debug.WriteLine("access the value of mParameter1 : " + AppSettings.Parameter1);
AppSettings.Parameter2 = theory.Description;
System.Diagnostics.Debug.WriteLine("access the value of mParameter2 : " + AppSettings.Parameter2);
lblName.Text = AppSettings.Parameter1;
lblDescription.Text = AppSettings.Parameter2;
}```
</details>
# 答案1
**得分**: 1
根据您的描述,建议您使用静态变量。
为此,您可以为您的应用程序创建一个静态全局类。然后,您可以在整个应用程序中访问和更新这些参数。
例如:
```csharp
public class AppSetting
{
public static string Parameter1 = "parameter1";
public static string Parameter2 = "parameter2";
}
然后,在您的应用程序的其他地方,您可以按如下方式访问和更新参数:
// 访问全局变量
string value1 = AppSetting.Parameter1;
System.Diagnostics.Debug.WriteLine("access the value of mParameter1 : " + value1);
// 更新设置参数的值
AppSetting.Parameter2 = "reset value for test";
System.Diagnostics.Debug.WriteLine("the updated value of Parameter2 " + AppSetting.Parameter2);
更新:
我发现您的代码放错了位置。
如果您想使用静态全局变量,请从您的代码中删除MessageCenter
。
您可以尝试将您的代码修改如下:
- 在
NewTheoryPage.xaml.cs
中,将变量AppSettings.Parameter1
和AppSettings.Parameter2
更改如下:
var response = await Services.NewTheory.PostNewActivitie(theory);
if (response.IsSuccessStatusCode)
{
//MessagingCenter.Send(this, "TheoryPublished", theory);
AppSettings.Parameter1 = theory.Name;
System.Diagnostics.Debug.WriteLine("access the value of mParameter1 : " + AppSettings.Parameter1);
AppSettings.Parameter2 = theory.Description;
System.Diagnostics.Debug.WriteLine("access the value of mParameter2 : " + AppSettings.Parameter2);
await DisplayAlert("Great", "The theory has been added", "Ok");
await Navigation.PopModalAsync();
}
- 在
SettingsPage
页面上,在OnAppearing
方法中加载数据:
protected override void OnAppearing()
{
base.OnAppearing();
lblName.Text = AppSettings.Parameter1;
lblDescription.Text = AppSettings.Parameter2;
}
更新 2:
一开始它有效,但是当我重新启动时它会消失
静态变量的生命周期与您的应用程序运行时间一样长。如果您希望在关闭应用程序后保留数据,建议您使用安全存储或首选项来实现此目的。
- 在
NewTheoryPage.xaml.cs
中,将值保存如下:
var response = await Services.NewTheory.PostNewActivitie(theory);
if (response.IsSuccessStatusCode)
{
//MessagingCenter.Send(this, "TheoryPublished", theory);
//AppSettings.Parameter1 = theory.Name;
//System.Diagnostics.Debug.WriteLine("access the value of mParameter1 : " + AppSettings.Parameter1);
//AppSettings.Parameter2 = theory.Description;
//System.Diagnostics.Debug.WriteLine("access the value of mParameter2 : " + AppSettings.Parameter2);
// 在这里保存数据
// 设置值:
Preferences.Default.Set("name", theory.Name);
Preferences.Default.Set("description", theory.Description);
await DisplayAlert("Great", "The theory has been added", "Ok");
await Navigation.PopModalAsync();
}
- 在
SettingsPage
页面上,在OnAppearing
方法中加载数据:
protected override void OnAppearing()
{
base.OnAppearing();
//lblName.Text = AppSettings.Parameter1;
//lblDescription.Text = AppSettings.Parameter2;
string name = Preferences.Default.Get("name", "Unknown");
string description = Preferences.Default.Get("description", "Unknown");
lblName.Text = name;
lblDescription.Text = description;
}
英文:
Based on your description, it is recommended that you use the static variable.
For this, you can create a static global class for your app. And you can access and update these parameters across your app.
For example:
public class AppSetting
{
public static string Parameter1 = " parameter1";
public static string Parameter2 = " parameter2";
}
Then in other places of your app, you can access and update your parameters as follows:
//access the global variable
string value1 = AppSetting.Parameter1;
System.Diagnostics.Debug.WriteLine("access the value of mParameter1 : " + value1);
//update the value of the setting parameter
AppSetting.Parameter2 = "reset value for test";
System.Diagnostics.Debug.WriteLine("the updated value of Parameter2 " + AppSetting.Parameter2);
Update:
I found that your code was misplaced.
If you want to use the static global variable, please remove the MessageCenter
in your code.
You can try to modify your code as follows:
1.In NewTheoryPage.xaml.cs
, change variable AppSettings.Parameter1
and AppSettings.Parameter2
as follows:
var response = await Services.NewTheory.PostNewActivitie(theory);
if (response.IsSuccessStatusCode)
{
//MessagingCenter.Send(this, "TheoryPublished", theory);
AppSettings.Parameter1 = theory.Name;
System.Diagnostics.Debug.WriteLine("access the value of mParameter1 : " + AppSettings.Parameter1);
AppSettings.Parameter2 = theory.Description;
System.Diagnostics.Debug.WriteLine("access the value of mParameter2 : " + AppSettings.Parameter2);
await DisplayAlert("Great", "The theory has been added", "Ok");
await Navigation.PopModalAsync();
}
2.On page SettingsPage
,load data on method OnAppearing
protected override void OnAppearing()
{
base.OnAppearing();
lblName.Text = AppSettings.Parameter1;
lblDescription.Text = AppSettings.Parameter2;
}
Update 2
> It works at the beginning but, when I restart it disappears
The life time of a static variable is as long as your application is running.
If you want to save the data even after closing the app, it is recommended that you use Secure storage or Preferences to achieve this.
1.In NewTheoryPage.xaml.cs
, save value as follows:
var response = await Services.NewTheory.PostNewActivitie(theory);
if (response.IsSuccessStatusCode)
{
//MessagingCenter.Send(this, "TheoryPublished", theory);
//AppSettings.Parameter1 = theory.Name;
//System.Diagnostics.Debug.WriteLine("access the value of mParameter1 : " + AppSettings.Parameter1);
//AppSettings.Parameter2 = theory.Description;
//System.Diagnostics.Debug.WriteLine("access the value of mParameter2 : " + AppSettings.Parameter2);
//save data here
// Set value here:
Preferences.Default.Set("name", theory.Name);
Preferences.Default.Set("description", theory.Description);
await DisplayAlert("Great", "The theory has been added", "Ok");
await Navigation.PopModalAsync();
}
2.On page SettingsPage
,load data on method OnAppearing
protected override void OnAppearing()
{
base.OnAppearing();
//lblName.Text = AppSettings.Parameter1;
//lblDescription.Text = AppSettings.Parameter2;
string name = Preferences.Default.Get("name", "Unknown");
string description = Preferences.Default.Get("description", "Unknown");
lblName.Text = name;
lblDescription.Text = description;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论