UWP - Platform::DisconnectedException while navigating between pages - c++

I've got the following setup: A MainPage xaml-view and a SettingPage xaml-view. In the SettingPage xaml-view I activated the back button which is in the window title bar and I added a BackRequestedEventArgs. (Furthermore I have a DX12 xaml page but it is not involved to the navigation yet, so it will never get initialized.)
So my problem is: if I click on a flyoutitem called settings which is located in the MainPage, I'll get navigated to the SettingPage. The backbutton appears in the titlebar and if I click it, I get back to the MainPage. Now I do it once again: clicking on settings, navigating to SettingPage. Now if I click on the backbutton OR close the window the app crashes and shows me the following exception:
Platform::DisconnectedException ^ at 0x046BED80. HRESULT:0x80010108
My Question: How do I fix it?
Here is my Code for it:
MainPage Navigation:
void MainPage::MenuFlyoutItemSettings_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(SettingsPage::typeid));
}
SettingsPage:
// in Constructor
Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->AppViewBackButtonVisibility = Windows::UI::Core::AppViewBackButtonVisibility::Visible;
Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->
BackRequested += ref new Windows::Foundation::EventHandler<
Windows::UI::Core::BackRequestedEventArgs^>(
this, &SettingsPage::App_BackRequested);
void SettingsPage::App_BackRequested(
Platform::Object^ sender,
Windows::UI::Core::BackRequestedEventArgs^ e)
{
Windows::UI::Xaml::Controls::Frame^ rootFrame = dynamic_cast<Windows::UI::Xaml::Controls::Frame^>(Window::Current->Content);
if (rootFrame == nullptr)
return;
// Navigate back if possible, and if the event has not
// already been handled.
if (rootFrame->CanGoBack && e->Handled == false)
{
e->Handled = true;
rootFrame->GoBack();
}
}
Furthermore both methods have onSuspending and onResuming handlers added by me manually, but they are both empty:
//in constructor
Application::Current->Suspending += ref new SuspendingEventHandler(this, &SettingsPage::OnSuspending);
Application::Current->Resuming += ref new EventHandler<Object^>(this, &SettingsPage::OnResuming);
void SettingsPage::OnSuspending(Object^ sender, SuspendingEventArgs^ e) {
}
void SettingsPage::OnResuming(Object^ sender, Object^ e) {
}
NOTE: If I delete the whole backbutton-code, the app never crashes with this exception, so I think it is an error in this code.
EDIT 2017-09-04:
After working on Sunteen Wu - MSFT's Answer from below I realised that even If I delete all the backbutton-code I will get this exception as soon as I enter the SettingsPage the first time and close the app. So here is my current scenario where I am getting the described exception:
The only code I've got now for navigation:
MainPage (in a custom settingsbutton):
this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(SettingsPage::typeid));
SettingsPage (in a custom backbutton):
this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(MainPage::typeid));
So after the first time I navigate to the settingspage by pressing the settingsbutton I get the described exception only if I shutdown the app (same if clicking on red x or stopping debugger). The navigation works fine though, I can swap between the pages as long as I want and I won't get the exception while running the app.
FINAL ANSWER 2017-09-06:
Combining Sunteen Wu - MSFT's Answer with deleting the above mentioned
Application::Current->Suspending += ref new SuspendingEventHandler(this, &SettingsPage::OnSuspending);
Application::Current->Resuming += ref new EventHandler<Object^>(this, &SettingsPage::OnResuming);
handlers is the solution for me. Now there is no Disconnectedexception and the Back-Button-Logic is also working!

Platform::DisconnectedException ^ at 0x046BED80. HRESULT:0x80010108
Actually you are meeting the cycles issue. For what it the cycles issue and how to resolve please reference Weak references and breaking cycles (C++/CX). You met the cycles issue when you subscribe the BackRequested event handle. With the WeakReference you will find the issue:
WeakReference wr(this);
Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->
BackRequested += ref new Windows::Foundation::EventHandler<
Windows::UI::Core::BackRequestedEventArgs^>([wr](
Object^ sender, Windows::UI::Core::BackRequestedEventArgs^ e)
{
SettingsPage^ c = wr.Resolve<SettingsPage>();
if (c != nullptr)
{
Windows::UI::Xaml::Controls::Frame^ rootFrame = dynamic_cast<Windows::UI::Xaml::Controls::Frame^>(Window::Current->Content);
if (rootFrame == nullptr)
return;
if (rootFrame->CanGoBack && e->Handled == false)
{
e->Handled = true;
rootFrame->GoBack();
}
}
else
{
throw ref new DisconnectedException();
}
});
From the article, when an event handler throws DisconnectedException, it causes the event to remove the handler from the subscriber list. So that to resolve it, you can remove the event handle from subscriber list after back requested. The following code snippet showed how to remove.
Windows::Foundation::EventRegistrationToken cookie;
SettingsPage::SettingsPage()
{
InitializeComponent();
...
cookie = Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->
BackRequested += ref new Windows::Foundation::EventHandler<
Windows::UI::Core::BackRequestedEventArgs^>(
this, &SettingsPage::App_BackRequested);
}
void SettingsPage::App_BackRequested(
Platform::Object^ sender,
Windows::UI::Core::BackRequestedEventArgs^ e)
{
...
Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->
BackRequested -= cookie;
}
Additionally, I recommend you subscribe this event inside App.xaml.cpp to avoid this issue. You can subscribe it inside OnLaunched like follows:
void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e)
{
auto rootFrame = dynamic_cast<Frame^>(Window::Current->Content);
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == nullptr)
{
...
}
else
{
...
}
Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->
BackRequested += ref new Windows::Foundation::EventHandler<
Windows::UI::Core::BackRequestedEventArgs^>(
this, &App::App_BackRequested);
}
void App::App_BackRequested(
Platform::Object^ sender,
Windows::UI::Core::BackRequestedEventArgs^ e)
{
...
}
More details you can reference BackButton official sample.

Related

Qt message box - show message box until timeout

I have to show the saving message box until timeout.
Once the timeout happens go to slot and do some function.
timerToSave=new QTimer(this);
connect(timerToSave,SIGNAL(timeout()),this,SLOT(SavingStatusSlot()));
Above code is the timer, when timeout moves to the saveslot.
bool PopUpManager::PopUpSaveStaus()
{
timerToSave->start(3000);
saveStatus=false;
if(SetThread::getInstance()->UISaveStatus==ST_PROCESSING)
{
msgBox = new QMessageBox(0);
msgBox->setModal(true);
msgBox->setText("Saving ... ");
msgBox->setIcon(QMessageBox::Information);
msgBox->setStandardButtons(QMessageBox::Ok);
msgBox->setCursor(Qt::WaitCursor);
msgBox->setWindowFlags(Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);
msgBox->setStyleSheet("background-color:#444;color:#FFF;outline:none;");
msgBox->exec();
}
else
SavingStatusSlot();
return saveStatus;
}
Above method called from other classes, when the user click on the save button.
once the method called, starting the timer then displaying the message box.
if timeout happens calling slot [given below]
void PopUpManager::SavingStatusSlot()
{
msgBox->button(QMessageBox::Ok)->animateClick();
timerToSave->stop();
if(SetThread::getInstance()->UISaveStatus==ST_OK)
{
saveStatus=true;
}
else
{
PopUpWithOKButton(" Saving Error ");
saveStatus=false;
}
}
this code is working, I have used the message box with OK button and when the timeout creating the animated click and doing some function.
Now I want to show the Message box without button and when timeout, close the message box then do some function
But the Message box close() is not working.
void PopUpManager::ClosePopUP()
{
if(msgBox->isEnabled())
msgBox->close();
}
if I call the above code the message box has to close, but it is displaying.
Can anyone help me on this.
Thanks in advance.
I have solved the issue
used msgBox->show(); instead of the msgBox->exec();
and msgBox->hide(); insted of msgBox->close();
code is given below.
bool PopUpManager::PopUpSaveStaus()
{
timerToSave->start(3000);
saveStatus=false;
if(UISaveStatus==ST_PROCESSING)
{
msgBox = new QMessageBox(QMessageBox::Information,"Error","Processing ... ",0,0,Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);
msgBox->setStandardButtons(0);
msgBox->setCursor(Qt::WaitCursor);
msgBox->setStyleSheet("background-color:#444;color:#FFF;outline:none;");
msgBox->show();
}
else
{
SavingStatusSlot();
}
return saveStatus;
}
void PopUpManager::SavingStatusSlot()
{
msgBox->hide();
timerToSave->stop();
if(UISaveStatus==ST_OK)
{
saveStatus=true;
}
else
{
PopUpWithOKButton(" communication Failed ");
saveStatus=false;
}
}

How to close the card by itself after executed a HTTP request?

I have an app with contextual commands. After triggered a contextual command, it will make a HTTP request with a link and post the result on the card, something like, "Completed!". I want this card to be closed by itself after one second so that the user need not to tap to close it. Once the result card is closed, it will go back to contextual command lists with "Ok, glass" at footer and ready for next command.
May i know how to do that?
private class HTTPRequest extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
try {
if (mWhat.equalsIgnoreCase("GET")) {
// get json via YouTube API
URL url = new URL("http://example.com");
mUrlConnection = (HttpURLConnection)
url.openConnection();
InputStream in = new BufferedInputStream(
mUrlConnection.getInputStream());
int ch;
StringBuffer b = new StringBuffer();
while ((ch = in.read()) != -1) {
b.append((char) ch);
}
mResult = new String(b);
}
} catch (Exception e) {}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mTvInfo.setText(mResult);
}
You can use an Android Dialog for this:
Use CardBuilder to create the "Completed" card using the MENU layout.
Create a new instance of Dialog and set its content view to be the view returned by CardBuilder.getView.
Show the dialog.
Use Handler.postDelayed (or some similar mechanism) to automatically dismiss the dialog after the desired amount of time has passed.

Launch a fragment in my Android application from the notification bar

How do I start a fragment in my Android application from a notification in the notification bar?
I've tried to implement this answer of creating my own action and then setting the action to the intent, but I'm unsure how to use it and what is required additionally - like adding something to the Manifest.
I've got a notification class that receives a context, a message and then an action. I then want to filter on that action to determine which fragment to launch, but I don't know how to launch a fragment as opposed to launching an activity.
Here is my Notifications.java class (incomplete):
public class Notifications {
private Context mContext;
public Notifications(Context context) {
this.mContext = context;
}
public static void notify(Context context, String message, String action) {
//Action you invent should include the application package as a prefix — for example: "com.example.project.SHOW_COLOR".
action = "my.package.name.here.frag."+action;
//Construct a user message.
String appName = context.getResources().getString(R.string.app_name);
// Use the Notification manager to send notification
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a notification using android stat_notify_chat icon.
Notification notification = new Notification(R.drawable.ic_stat_notification, message, 0);
//Sound, lights, vibration.
//REMEMBER PERMISSIONS.
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
// Create a pending intent to open the application when the notification is clicked.
//Restart the app.
Intent launchIntent = null;
//Get the action and based on what the action is, launch the application displaying the appropriate fragment.
if (action.equalsIgnoreCase("friend")){
//New friend notification
//Launch application displaying the list of friends
}
if (action.equalsIgnoreCase("article")){
//New article has been posted
//Launch application displaying the news feed fragment
}
if (action.equalsIgnoreCase("points")){
//Points scored notification
//Launch application displaying the user's profile
}
if (action.equalsIgnoreCase("redeemable")){
//New redeemable is offered
//Launch application displaying the list of redeemables
}
if (!action.equalsIgnoreCase("friend")
&& !action.equalsIgnoreCase("article")
&& !action.equalsIgnoreCase("points")
&& !action.equalsIgnoreCase("redeemable")){
//Not specific, so launch the application from scratch displaying the activity feed
launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
}
if(action != null && launchIntent != null){
launchIntent.setAction(action);
}
// Set the notification and register the pending intent to it
notification.setLatestEventInfo(context, appName, message, pendingIntent);
// Trigger the notification
notificationManager.notify(0, notification);
}
}
So this was actually pretty easy. Hopefully I can help someone else see this too.
I send an action to this notify function. The I add that action to my intent to launch an activity. In my case I open the launching activity, because all the fragments are loaded from within that activity based on what the user does. So I set the action using setAction and the I use that action in the activity as below.
My Notifications.java class changed to this:
public static void notify(Context context, String message, String action) {
action = action.toUpperCase();
// Create a pending intent to open the the application when the notification is clicked.
//Restart the app.
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if(action != null && launchIntent != null){
launchIntent.setAction(action);
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Set the notification and register the pending intent to it
notification.setLatestEventInfo(context, appName, message, pendingIntent);
// Trigger the notification
notificationManager.notify(0, notification);
}
And then in my activity from where I load the fragments, I get the action and filter it:
Intent intent = getIntent();
try{
String action = intent.getAction().toUpperCase();
if(action != null){
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_friend))){
goFrag(getResources().getInteger(R.integer.FRAG_A_INT));
}
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_article))){
goFrag(getResources().getInteger(R.integer.FRAG_B_INT));
}
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_points))){
goFrag(getResources().getInteger(R.integer.FRAG_C_INT));
}
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_redeemable))){
goFrag(getResources().getInteger(R.integer.FRAG_D_INT));
}
if(action.equalsIgnoreCase(getResources().getString(R.string.notification_action_dance))){
goFrag(getResources().getInteger(R.integer.FRAG_E_INT));
}
}else{
Log.d(TAG, "Intent was null");
}
}catch(Exception e){
Log.e(TAG, "Problem consuming action from intent", e);
}
In my goFrag function I replace the fragment if the required fragment is still in memory (meaning the user was there earlier and it hasn't been destroyed yet), or I create a new fragment required.

need help in translating silverlight 4 code to silverlight 5

I am extremely new to silverlight working on a new control called pivotviewer in silverlight 5.
I am trying to write a doubleclick event for the pivot viewer from an existing silverlight 4 code.
//double click event
private void PivotViewerControl_ItemDoubleClicked(object sender, ItemEventArgs e)
{
PivotItem piv_item =PivotViewerControl1.GetItem(e.ItemId);
if (!string.IsNullOrWhiteSpace(piv_item.Href))
{
PivotViewerControl1.CurrentItemId = e.ItemId;
OpenLink(piv_item.Href);
}
else
{
MessageBox.Show("No Web Page...");
}
}
while translating this to silverlight 5 where pivotviewer is inbuilt control the following stub is created for the event.
private void PivotViewerControl_ItemDoubleClicked(object sender, PivotViewerItemDoubleClickEventArgs e)
{
//here the pivotviewercontrol has not getitem() in silverlight 5 so How do i get the currently selected
//Item on the double click
}
Also I am not getting the images on the page load itself. My page load code is as follows
public MainPage()
{
InitializeComponent();
PivotViewerControl.Loaded += PivotViewerControl_Loaded;
}
void PivotViewerControl_Loaded(object sender, RoutedEventArgs e)
{
_cxml = new CxmlCollectionSource(new Uri(MauritiusCollectionUri, UriKind.RelativeOrAbsolute));
_cxml.StateChanged += _cxml_StateChanged;
}
void _cxml_StateChanged(object sender, CxmlCollectionStateChangedEventArgs e)
{
if (e.NewState == CxmlCollectionState.Loaded)
{
PivotViewerControl.PivotProperties = _cxml.ItemProperties.ToList();
PivotViewerControl.ItemTemplates =_cxml.ItemTemplates;
PivotViewerControl.ItemsSource =_cxml.Items;
}
}
On running I am only getting the images when I sort or do some search can anyone tell me why? Thanks
Hi the double click event I have able to translate is as follows
private void PivotViewerControl_ItemDoubleClicked(object sender,
PivotViewerItemDoubleClickEventArgs e)
{
PivotViewerItem piv_item = (PivotViewerItem)e.Item;
if(piv_item.Properties[0]!=null && piv_item.Properties[0].ToString()!=string.Empty)
{
OpenLink(piv_item.GetPropertyValue("Href")[0].ToString());
}
}

AddNewRecord XamDataGrid

After entering a value in the AddNewRecord row, and clicking anywhere outside the row on the XamDataGrid seems to add the row to the collection.
How do I prevent mouse click from adding a new row to the collection.
Kindly any help
Clicking outside of the AddNewRecord ends edit mode on the record and if there were changes they are committed at that time which means the new record is added. If you were looking to only allow the record to be commmited when pressing the enter key and not by clicking another record in the grid, then you could use the following logic to set the mouse left button down as handled:
private bool editingAddNewRecord = false;
void XamDataGrid1_EditModeEnded(object sender, Infragistics.Windows.DataPresenter.Events.EditModeEndedEventArgs e)
{
this.editingAddNewRecord = false;
}
void XamDataGrid1_EditModeStarted(object sender, Infragistics.Windows.DataPresenter.Events.EditModeStartedEventArgs e)
{
this.editingAddNewRecord = e.Cell.Record.IsAddRecord;
}
void XamDataGrid1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (editingAddNewRecord)
{
DataRecordPresenter drp = Infragistics.Windows.Utilities.GetAncestorFromType(e.OriginalSource as DependencyObject, typeof(DataRecordPresenter), true) as DataRecordPresenter;
if (!(drp != null && drp.IsAddRecord))
{
e.Handled = true;
}
}
}
Thanks for the answer #alhalama!
I noticed though that you don't handle the right mouse button down, and even when we do your solution doesn't work to support it. Also, with your solution I wasn't able to edit any other cells until I had hit Enter or Escape on the Add New Row record (which might be what some people want, but not me). Here is my modified solution that undoes changes to the Add New Record row's cell when the user clicks out of it, which also handles all mouse clicks (left, right, middle, etc.).
// Used to record when the user is editing a value in the Mass Edit row.
private DataRecord _addRecordCellBeingEdited = null;
private void XamDataGrid1_EditModeStarted(object sender, Infragistics.Windows.DataPresenter.Events.EditModeStartedEventArgs e)
{
if (e.Cell.Record.IsAddRecord)
_addRecordCellBeingEdited = e.Cell.Record;
}
private void XamDataGrid1_EditModeEnded(object sender, Infragistics.Windows.DataPresenter.Events.EditModeEndedEventArgs e)
{
_addRecordCellBeingEdited = null;
}
private void XamDataGrid1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (_addRecordCellBeingEdited != null)
{
DataRecordPresenter drp = Infragistics.Windows.Utilities.GetAncestorFromType(e.OriginalSource as DependencyObject, typeof(DataRecordPresenter), true) as DataRecordPresenter;
if (!(drp != null && drp.IsAddRecord))
{
_addRecordCellBeingEdited.CancelUpdate();
}
}
}