I have a problem. I have 2 fragments with methods. Now I want to call a method from Fragment2.cs inside Fragment1.cs. Here is the method from Fragment2.cs:
public void UpdateActionBar(int CurrentFragmentNum)
{
if (CurrentFragmentNum == 1)
{
btnBack.Visibility = ViewStates.Invisible;
btnNext.Visibility = ViewStates.Invisible;
}
else
{
btnBack.Visibility = ViewStates.Visible;
btnNext.Visibility = ViewStates.Visible;
}
if (CurrentFragmentNum == 3)
{
btnNext.Text = "Finish";
}
else
{
btnNext.Text = "Next";
}
}
And in Fragment1.cs I need to call this method! How can I do that... I already know how to call a method from the MainActivity using this:
(Activity as MainActivity)?.Method();
But now I want the method from another fragment.
I have already tried something like this:
(Fragment as Fragment2)?.Method();
But that gives me the error:
'Fragment' is a type, which is not valid in the given context
Can someone help me?
Please make search before ask, the question is very popular
google search
Related
I searched everywhere, but i can't find a tutorial for my problem. I want to set an page to be shown, when the App is launched for the first time. something like th:
First launch:
Greeting.xaml>Setting.xaml>MainPage.xaml
Regular launch goes directly to MainPage.
how can i do this?
I didn't mean a Splashscreen, I mean a page, which is shown only the first time you launch the App, something like a little tutorial.
Your typical template-generated App.xaml.cs has something like this in its OnLaunched method:
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
This is where you navigate to your first page. To special-case a first run, do something like this instead:
if (rootFrame.Content == null)
{
IPropertySet roamingProperties = ApplicationData.Current.RoamingSettings.Values;
if (roamingProperties.ContainsKey("HasBeenHereBefore"))
{
// The normal case
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
else
{
// The first-time case
rootFrame.Navigate(typeof(GreetingsPage), e.Arguments);
roamingProperties["HasBeenHereBefore"] = bool.TrueString; // Doesn't really matter what
}
}
The greetings page should then navigate to your settings page, which should navigate to your main page.
And by using the roaming settings, the user won't see the first-time screen when she logs in to a different machine.
You can set the "first" page within the App.xaml.cs. Search for the OnLaunched void and change rootFrame.Navigate(typeof(MainPage)); to rootFrame.Navigate(typeof(Greeting)); or whtatever you like to call it.
The next step would be to check if the app launches for the first time. You can set an app setting to do that.
1. create the OnnavigatedTo void for your Greeting.xaml (just type "protected override void onna", IntelliSense will suggest it to you) and make is asynchron by inserting "async" after "protected", 2. use this code:
if (ApplicationData.Current.LocalSettings.Values.ContainsKey("isFirstLaunch"))
{
// if that's the first launch, stay, otherwise navigate to Settings.xaml
if (!(bool)ApplicationData.Current.LocalSettings.Values["isFirstLaunch"])
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => Frame.Navigate(typeof(Settings)));
}
}
else
{
ApplicationData.Current.LocalSettings.Values["isFirstLaunch"] = false;
}
I haven't tested the code but it should work. If it doesn't, just ask me.
Edit: here's a much better solution :D https://stackoverflow.com/a/35176403/3146261
I just wanted a Disclaimer to be accepted via a MessageBox
IPropertySet roamingProperties = ApplicationData.Current.RoamingSettings.Values;
if (!roamingProperties.ContainsKey("DisclaimerAccepted"))
{
var dialog = new MessageDialog(strings.Disclaimer);
dialog.Title = "Disclaimer";
dialog.Commands.Clear();
dialog.Commands.Add(new UICommand { Label = "Accept", Id = 0 });
dialog.Commands.Add(new UICommand { Label = "Decline", Id = 1 });
var result = await dialog.ShowAsync();
if ((int)result.Id == 1)
Application.Current.Exit();
roamingProperties["DisclaimerAccepted"] = bool.TrueString;
}
I placed it in App.xaml.cs inside of:
if (e.PrelaunchActivated == false)
{
<Inside here>
if (rootFrame.Content == null)
{
}
This is a bit hard to explain, but I'm hoping this example will clear it up.
Say I have some function call Visible:
public bool Visible(/* Some page element */)
{
// Checks if something on a webpage is visible. Returns a "true" is yes, and "false" if not
}
Is it possible to some how wait for this function to return true? What I've written out so far looks like this:
public void WaitUntil(/*function returning bool*/ isTrue)
{
for (int second = 0; ; second++)
{
if (second >= 12)
{
/* Thow exception */
}
else
{
if (isTrue /*calls the isTrue function with given parameters*/)
{
return;
}
}
}
}
Such that these two method could be used together like:
WaitUntil(Visible(/* Some page element */));
to wait until a page element is visible... Is this possible?
Here is how to do it (although you should consider using events as this kind of "waiting" is strongly discouraged)
/*Important Note: This is ugly, error prone
and causes eye itchiness to veteran programmers*/
public void WaitUntil(Func<bool> func)
{
DateTime start = DateTime.Now;
while(DateTime.Now - start < TimeSpan.FromSeconds(12))
{
if (func())
{
return;
}
Thread.Sleep(100);
}
/* Thow exception */
}
//Call
WaitUntil(() => Visible(/* Some page element*/));
I have a label and I want it to display either Player or Console depending on what the variable answer is.
private void playerLabel_Click(object sender, EventArgs e)
{
string playerDetail = "Player",
consoleDetail = "Console";
if (Class.Method.Variable == 1)
{
Show.playerDetail();
}
if else (Class.Method.Variable == 0)
{
Show.consoleDetail();
}
}`
I then want to make it so that the label shows the string instead if you get me. I know I am not doing this properly but I can't work out how exactly to do this.
private void playerLabel_Click(object sender, EventArgs e)
{
string labelText = playerLabel.Text;
if (Class.Method.Variable == 1)
{
labelText = "Player";
Show.playerDetail();
}
else if(Class.Method.Variable == 0)
{
labelText = "Console";
Show.consoleDetail();
}
playerLabel.Text = labelText;
}
It would be better if your methods in Show class returned the appropriate string, so that you can do: playerLabel.Text = Show.WhateverDetail();. Additionally its even better if you could tie the Show method with the Variable value so that you don't have to use an if-else logic at all.
a. The Text property of the Label is what you want to set your strings to.
playerLabel.Text = playerDetail;
playerLabel.Text = consoleDetail;
b. Your if/else method should be in the form of:
if (test)
{
}
else if
{
}
else
{
}
You don't need the else if bit in the middle if there are only two branches.
c. I'm not sure about Show.consoleDetail() and Show.playerDetail(). Are 'consoleDetail()' and 'playerDetail()' method calls?
This is my javascript method in .aspx file. I want to invoke this method from code-behind based on a certain condition:
function confirmboxAndHideMessage() {
//HideMessage();
var response = confirm("are you sure?.");
if (response == true) {
document.getElementById("<%=chkValidated.ClientID%>").checked = true;
HideMessage();
}
else {
HideMessage();
return true;
}
}
The condition upon which I want to invoke is something like this:
if (obj.fkValidationStatusId.HasValue && obj.fkValidationStatusId.Value.Equals(1))
{
btnProceedAddNewRecords.Attributes.Add("OnClick", "javascript:return confirmboxAndHideMessage();");
}
else
{
btnProceedAddNewRecords.Attributes.Remove("OnClick");
}
This condition is being exercised in a method which is called in PageLoad event inside
if (!IsPostBack) { /* condition */ }
It is not working and my guess is that the way i am adding the method in button attribute is wrong. My request is that, kindly suggest a way I can invoke this javascript method from my code-behind based on the stated condition. If you think that my approach is flawed, Please do suggest Alternatives. Thanks.
use to set/unset OnClientClick
if (obj.fkValidationStatusId.HasValue && obj.fkValidationStatusId.Value.Equals(1))
{
btnProceedAddNewRecords.OnClientClick="return confirmboxAndHideMessage();";
}
else
{
btnProceedAddNewRecords.OnClientClick="retun false;";
}
Just add the following code where the condition is like
public void test()
{
String name = "test";
if (name == "test")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "hdrEmpty", "if(confirm('are you sure you want to continue?')==true){ Testfunction(); };", true); return;
}
}
I am sure it will work for you.
You should return true or false from the function. You probably want to return true for a positive response:
function confirmboxAndHideMessage() {
var response = confirm("are you sure?.");
if (response == true) {
document.getElementById("<%=chkValidated.ClientID%>").checked = true;
HideMessage();
return true;
} else {
HideMessage();
return false;
}
}
Use lowercase for the event name (as XHTML is picky about that), and remove javascript: from the code:
btnProceedAddNewRecords.Attributes.Add("onclick", "return confirmboxAndHideMessage();");
Put the code that sets the attribute outside the check for IsPostBack, otherwise it will not be updated when the condition changes.
Having been given the all clear to move code from my Page_Loaded method to the constructor (See HERE), i am now encountering errors on my Linq to entities query. It is now causing a nullreferenceexception and i can't figure out why at the moment. See below for the exception location.
public Building()
{
InitializeComponent();
lvBuildings.ItemsSource = App.ocBuildings;
getBuildings();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
}
private void getBuildings()
{
App.ocBuildings.Clear();
var tehBuildings = from building in App.ents.Buildings
where building.Organisations.OrganisationID == App.selectedOrganisation.OrganisationID
select building;
foreach (Buildings addBuilding in tehBuildings (<--Exception))
{
App.ocBuildings.Add(addBuilding);
}
}
Anyone have any ideas?
Thanks,
Kohan.
It looks like one of the following items is evaluating to null
App.ents
App.ents.Buildings
building.Organisations
App.SelectedOrganisation
This would cause a NullReferenceException to be thrown in the for each loop because the query is not actually evaluated until it is used.
We'll need some more information from you as to which one is null.
#JaredPar.
Fixed thanks to you, i would have never thought to look at the previous page in my client (that i was loading from) for the answer.
I had a button with the code:
Page newPage;
if (App.ModeType == "Mode1"){ newPage = new MyClient.Pages.Mode1.Building(); }
else if (App.ModeType == "Mode2") { newPage = new MyClient.Pages.Mode2.RiskQuestions(); }
else { throw new NotImplementedException(); } ///Must be Mode3
Organisations thisOrg = (Organisations)lvOrganisations.SelectedItem;
App.selectedOrganisation = thisOrg;
NavigationService.Navigate(newPage);
By moving App.selectedOrganisation to before i set the "newPage" everything was fixed. I guess that the constructor gets called at the point i was setting "newPage".
For the sake of clarity, Final code which worked for me looks like this:
Organisations thisOrg = (Organisations)lvOrganisations.SelectedItem;
App.selectedOrganisation = thisOrg;
Page newPage;
if (App.ModeType == "Mode1"){ newPage = new MyClient.Pages.Mode1.Building(); }
else if (App.ModeType == "Mode2") { newPage = new MyClient.Pages.Mode2.RiskQuestions(); }
else { throw new NotImplementedException(); } ///Must be Mode3
NavigationService.Navigate(newPage);