I want to pass ObversableCollecion temp from page1 to page2 ,when I click the button in page1.But I don't wan't to use MVVM,just be simnple.My code are as follows:
page1:
public ObservableCollection<Staff> Staff_Collection { get; set; }
private void button_click1(object sender, RoutedEventArgs e)
{
Page1 page1= new Page1();
page1.Staff_Show = Staff_Collection;
this.NavigationService.Navigate(page1);}
page2:
public ObservableCollection<Staff> Staff_Show = new ObservableCollection<Staff>();
public TaxBefore_Sum()
{
InitializeComponent();
DataContext = this;
Staff_Show = new ObservableCollection<Staff>();
}
but it doesn't work.
var whateverPage = new WhateverPage(whateverDataYouWantToPass);
this.NavigationService.Navigate(whateverPage);
Just make sure, the constructor for WhateverPage (the page you are navigating to) is overloaded to accept the item you are passing into it like this:
public WhateverPage(WhateverData data)
{
// code here
}
So in your case, if you are navigating to a page called Page2, then you will create a constructor in that page like this:
public Page2(ObservableCollection<Staff> staff)
{
// Now you have staff in Page2. You can assign it to a property or field
}
Related
is there way to pass value in Rg.Plugin.Popup popupNavigation?
I have a page, with a button. if clicked, I want to open custom popup page, with passing a ID value
CustomPopupPage _CustomPopupPage = new CustomPopupPage();
PopupNavigation.Instance.Pushing += (sender, e) => Debug.WriteLine($"[Popup] Pushing: {e.Page.GetType().Name}");
var ID = "5";
await PopupNavigation.Instance.PushAsync(_CustomPopupPage);
custompopuppage class - here i want Get the ID and do something with it
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CustomPopupPage : Rg.Plugins.Popup.Pages.PopupPage
{
...
}
As Jason's reply, you can pass value in Popup page's constructor, For example, this is the Popup Page.
public partial class popup1 : PopupPage
{
string parameter;
public popup1(string str)
{
InitializeComponent();
//get pass value from contentpage,
parameter = str;
}
}
Pass value from ContentPage.
private async void btnPopupButton_Clicked(object sender, EventArgs e)
{
await PopupNavigation.Instance.PushAsync(new popup1("parameter Id"));
}
I am new to Xamarin and MVVM. In my application I have Tabbed page and I have two child views inside. I am making a network call and getting data from databse in my first child's view model. Data is coming. I wanted to pass some data to my second child(List). I was able to pass data correctly. Inside my second child, I have a button and calling a new Page.
First child's ViewModel...
public async Task getFypDataAsync(string accessToken)
{
fypFullList = await _apiServices.GetFypData(accessToken);
fypFullList.Sort((x, y) => x.rank.CompareTo(y.rank));
fypSendList.AddRange(new List<FypRankData>(fypFullList.GetRange(0, 320)));
new HomeTab2(fypSendList); // send data to second child
}
List's data is available inside my second child's constructor (code behind).
Second child's code behind....
public partial class HomeTab2 : ContentPage
{
bool _istapped = false;
public List<FypRankData> mylist = new List<FypRankData>();
public HomeTab2()
{
InitializeComponent();
BindingContext = new HomeTabVM2();
var vm = BindingContext as HomeTabVM2;
vm.setFypData();
}
public HomeTab2(List<FypRankData> fypSendList)
{
mylist = fypSendList;
Debug.WriteLine(fypSendList.Count.ToString(), " list_yyycount ");
}
private async void Btn1_Clicked(object sender, EventArgs e)
{
if (_istapped)
return;
_istapped = true;
await Navigation.PushAsync(new ChartPage(mylist));
_istapped = false;
}
}
Problem is inside the button click mylist is getting empty. But inside the constructor, value is assigning.
I have a method, where I call a new Windows Form in Class A. And in the new Form, I use a Dropdown menu and store the selected Item from the Dropdown in a variable, called selectedItem.Now I have to access this selectedItem in Class A. I use the following code.
public class A
{
public method callingmethod()
{
ExceptionForm expform = new ExceptionForm();
expform.Show();
string newexp = expobj.selectedexception;
}
}
And my code in New Form,
public partial class ExceptionForm : Form
{
public string selectedexception = string.Empty;
private void btnExpSubmit_Click(object sender, EventArgs e)
{
selectedexception = this.comboBox1.GetItemText(this.comboBox1.SelectedItem);
this.Close();
return;
}
}
Now After clicking on Submit button, I get the correct value in selectedItem, But I could not pass it to Class A. How to retun to Class A?
If you are ok with posting ExceptionForm over parent form by disabling it, go for ShowDialog. But, if you do not wish to disable parent for and continue popping ExceptionForm as a new and independent window, try eventing back to parent form. Here I show an example on how to do so:
public partial class ExceptionForm : Form
{
public delegate void SelectValueDelegate(string option);
public event SelectValueDelegate ValueSelected;
private void btnExpSubmit_Click(object sender, EventArgs e)
{
this.Close();
if (this.ValueSelected!= null)
{
this.ValueSelected(this.comboBox1.GetItemText(this.comboBox1.SelectedItem));
}
return;
}
}
And in calling class:
public class A
{
public method callingmethod()
{
ExceptionForm expform = new ExceptionForm();
expform.ValueSelected += ExceptionForm_ValueSelected;
expform.Show();
}
private void ExceptionForm_ValueSelected(string option)
{
string newexp = option;
// Do whatever you wanted to do next!
}
}
Use ShowDialog() method.
expform.ShowDialog();
string newexp = expobj.selectedexception;
Beginner here.
I have a list on my MainPage that I am adding items to by typing stuff into a textbox submitting it. After creating a listview item, I would like to be able to click on an item and see its value in another page.
I created a DetailsPage for this, but I don't know what the best way is to share that data across these pages.
This is what I have in my MainPage so far:
public sealed partial class MainPage : Page
{
public int itemCounter;
public MainPage()
{
this.InitializeComponent();
//This is the button that submits the entered text
public void btnSubmitInPopup_Click_1(object sender, RoutedEventArgs e)
{
//Create new listview item and textblock
ListViewItem item = new ListViewItem();
TextBlock txtBloodSugarValue = new TextBlock();
//Save text input value to label and add item to list
txtBloodSugarValue.Text = txtInput.Text;
item.Content = txtBloodSugarValue;
mainListView.Items.Add(item);
//Reset text input field
txtInput.Text = "";
//Update counter of items in list
itemCounter = mainListView.Items.Count();
lblItemCounter.Text = itemCounter.ToString();
}
}
My DetailPage literally has just one label that I am trying to populate with whatever value is in the listview item that I clicked on in my MainPage.
What's the best way to do this?
Hope this makes sense.
Thanks so much.
You have 3 way to do that:
declaring static variable and set that in first form and use in second form(or anywhere!)
Create public field/propeey inside second form class and set it from first form
Pass value as parameter to constructor/function of second class
I add second form class here:
public static int StaticVariable { get; set; }//First Method
public int PublicProperty { get; set; }
public Form2(int Value)
{
InitializeComponent();
//Do your code here with constructor way here
}
public Form2()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//Do your code for 1,2 here
}
public void SetValueWithFunction(int value)
{
//Do your code for setting value with second type in Number 3
}
I hope this helps :)
I have a parent form and a dialog. I need to pass info from the parent to the dialog
Here's what I have:
private void Item_Click(object sender, EventArgs e)
{
DialogResult result = DialogResult.OK;
DlgGraphOptions _frmDlgGraphOptions = new DlgGraphOptions();
_frmDlgGraphOptions.m_SerOpts = theDGroup.m_SerOpts;
_frmDlgGraphOptions.ShowDialog(this);
if (result == DialogResult.OK)
{
// Save the revised options to the Data Group
theDGroup.m_SerOpts = _frmDlgGraphOptions.m_SerOpts;
}
In DlgGraphOptions(child/dialog) form, I have intialitzed
public partial class DlgGraphOptions : Form
{
public GraphOpts_t m_SerOpts = new GraphOpts_t();
}
private void InitSettings(int idxSeries)
{
m_nMaxPts = m_SerOpts.GetMaxPts(idxSeries);
}
So I need to pass theDGroup.m_SerOpts from parent to the dialog,so I have done
_frmDlgGraphOptions.m_SerOpts = theDGroup.m_SerOpts;
in the parent. Now in the child:
public GraphOpts_t m_SerOpts = new GraphOpts_t;
This seems to be wrong. I don't want to be reinitializing it.
I think you should change your code in this way:
First, in the DlgGraphOptions form, change the constructor of DlgGraphOptions
// Force the caller to pass a GraphOpts_t
// Check if it is a valid instance or create one as new
public partial class DlgGraphOptions(GraphOpts_t input ) : Form
{
m_SerOpts = (input == null ? new GraphOpts_t() : input);
}
then create a public property with only the getter returning the internal GraphOpts
public GraphOpts_t Options
{
get{ return m_SerOpts; }
}
then, in the calling form, change uour code
// Pass the m_setOpts from theDGroup
DlgGraphOptions _frmDlgGraphOptions = new DlgGraphOptions(theDGroup.m_SerOpts);
if(DialogResult.OK == _frmDlgGraphOptions.ShowDialog(this))
{
// Save the revised (or new) options to theDGroup
theDGroup.m_SerOpts = _frmDlgGraphOptions.Options;
}
This approach will force the user of your dialog to pass an initialization value or null.
However your InitSettings will work with a initialized value and you don't have initialized two times your options instance.
(Actually there isn't a big improvement from your code, but I think it is a better approach)
Your child class should probably have the m_SerOpts as a property:
public partial class DlgGraphOptions : Form
{
public GraphOpts_t m_SerOpts { get; set; }
}
Your click event can probably be cleaned up like this:
private void Item_Click(object sender, EventArgs e)
{
using (DlgGraphOptions _frmDlgGraphOptions = new DlgGraphOptions()) {
_frmDlgGraphOptions.m_SerOpts = theDGroup.m_SerOpts;
if (_frmDlgGraphOptions.ShowDialog(this) == DialogResult.OK)
{
// Save the revised options to the Data Group
theDGroup.m_SerOpts = _frmDlgGraphOptions.m_SerOpts;
}
}
}
where in your DlgGraphOptions form, you need to set the form's DialogResult property in the OK or Save button event.
You could also just pass m_SerOpts object through the constructor:
public partial class DlgGraphOptions : Form
{
public GraphOpts_t m_SerOpts { get; }
public DlgGraphOptions(GraphOpts_t serOpts) {
InitializeComponents();
m_SerOpts = serOpts;
}
}