GetToastCollectionManager crash - c#

I already have Toast Notifications working, but I need to create a collection for them.
Following the code example in : https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/toast-collections
I get a crash on the GetToastCollectionManager call.
System.Exception
HResult=0x80070490
Message=Element not found. (0x80070490)
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
This exception was originally thrown at this call stack:
WpfApp1.MainWindow.CreateToastCollection() in MainWindow.xaml.cs
I'm not sure if I'm missing something obvious , but I cannot find examples or tutorials on Toast Notifications.
Any Ideas ?
public async void CreateToastCollection()
{
ToastNotificationManagerForUser defaultManager = ToastNotificationManager.GetDefault();
ToastCollectionManager collectionManager = defaultManager.GetToastCollectionManager();
string displayName = "Is Potato";
string launchArg = "NavigateToPotato";
System.Uri iconURI = new System.Uri("ms-appx:///Assets/icon.png");
ToastCollection licensingManagerToastCollection = new ToastCollection(
"MyToastCollection",
displayName,
launchArg,
iconURI);
// Calls the platform to create the collection
await collectionManager.SaveToastCollectionAsync(licensingManagerToastCollection);
}

Related

Selenium Unable to catch StaleElementReferenceException

I'm specifically asking what the issues may be with my try/catch block, which isn't described in the issues linked as "duplicates" to this.
I'm having an issue where I'm trying to catch a StaleElementReferenceException and then look up the element again but for some reason the exception isn't being caught. I have the method below and when I run the test I get a StaleElementReferenceException when this line is executed, value = element.GetAttribute(attributeName);. I assumed (maybe poorly) that adding the check in a try/catch and specifically looking for the exception would allow me to continue trying until selenium is able to find the element again. The issue is for some reason the exception isn't getting caught and the test immediately exits. Oddly, if I change the catch block to just catch the general Exception it works just fine. My concern with that is though I could get in a loop that would never exit. I'm using a page object model to initially initialize the elements.
bool isStale = false;
string value = "";
do
{
try
{
value = element.GetAttribute(attributeName);
isStale = false;
}
catch (StaleElementReferenceException)
{
element = driver.FindElement(By.XPath(xPath));
isStale = true;
}
} while (isStale == true);
return value;
This is part of the stack trace:
Test failed with error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=71.0.3578.98) (Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 10.0.17134 x86_64) at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Remote.RemoteWebElement.Execute(String commandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Remote.RemoteWebElement.GetAttribute(String attributeName)

C# Invoking Exception TargetSite

I have an application that can sometimes throw an exception, most of these are handled but I want the unhandled ones to be recoverable. So I'm trying to invoke the method that caused the exception by using the exception's targetsite like so:
Exception ex = Global.ThrownException;
MethodBase mB = ex.TargetSite;
try
{
mB.Invoke(mB, null);
}
catch(Exception exc)
{
System.Windows.MessageBox.Show(exc.Message);
}
I'm doing this to make sure that the exception was a one time error before showing the window to the user again.
The test method (and the exception targetsite) I'm trying to invoke is this:
public void testMethod()
{
throw new System.IO.IOException("test");
}
When I run this, an exception is thrown with the message "Object does not match target type" but since testMethod doesn't have any parameters this shouldn't happen.
Any ideas?

How to fix "System.Reflection.TargetInvocationException has been thrown"

I'm following a tutorial for making a discord bot, and after following the basics for setting up a bot I keep running into this exception:
System.Reflection.TargetInvocationException has been thrown. Exception has been thrown by the target of an invocation.
This is the code I have:
using Discord.Commands;
using Discord;
using System;
namespace TestBot
{
public class MyBot
{
DiscordClient discord;
public MyBot()
{
discord = new DiscordClient(x =>
{
x.LogLevel = LogSeverity.Info;
x.LogHandler = Log;
});
discord.UsingCommands(x =>
{
x.PrefixChar = '!';
x.AllowMentionPrefix = true;
});
var commands = discord.GetService<CommandService>();
commands.CreateCommand("test")
.Do(async (e) =>
{
await e.Channel.SendMessage("response");
});
discord.ExecuteAndWait(async () =>
{
await discord.Connect("BOT_TOKEN", TokenType.Bot);
});
}
private void Log(object sender, LogMessageEventArgs e)
{
Console.WriteLine(e.Message);
}
}
}
MonoDevelop says that the exception is happening with this piece of code:
discord.ExecuteAndWait(async () =>
{
await discord.Connect("BOT_TOKEN", TokenType.Bot);
});
I don't know how to fix this, so any help would be greatly appreciated.
(As this is my first post here, any suggestions on how to improve my posts are also appreciated.)
The true cause for your exception is being hidden from you. To fix this issue you will need to view the inner exception (and if required the inner exception of that exception and so on..). This will allow you to see the root cause of the issue and allow you to workout how to fix it.
You can view the inner exception when your code breaks in debug mode;
Clicking view detail will allow you to see more information about your exception;
Once you get to the true cause of your exception I suggest you post a new question asking how to solve that specific issue (if you can't solve it yourself).
In my case, it's caused by some exception in some inner code, and the exception was throwed up through the stack, when come to the surface and show exception window, the exception message has been losed so it shows a useless message System.Reflection.TargetInvocationException has been thrown.
The solution is, open Exception Settings Window, and check all items so that it'll break when any type of Exception happens, so you can catch the exception at the most initial position that the error reason can be esaily undederstood.
My idea comes from HERE.

Get Method, Class and LineNumber from StatckTrace in Application_UnhandledException event

I am developing Windows Phone 7 Silverlight Application. I want to do Application Level error handling instead of writing try...catch... in all methods. I need to extract Method Name, Class Name and Line Number where the actual error occurred. Below is the demo code. In Application_UnhandledException event, I am expecting Method = "GenerateError" and Class = "ExceptionTesting". Also, I want to get LineNumber where the actual error occurred (this is not shown in code).
Code to generate Error:
public partial class ExceptionTesting : PhoneApplicationPage
{
// Generate Error to Test Exception Handling
private void GenerateError()
{
Int16 i = Convert.ToInt16("test");
}
}
Code that Handles Application Level Exception:
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
StackTrace st = new StackTrace();
var query = st.GetFrames() // get the frames
.Select(frame => new
{
Method = frame.GetMethod(),
Class = frame.GetMethod().DeclaringType
});
foreach (var q in query)
{
if (q.Method.Name.Contains("GenerateError"))
{
MessageBox.Show("Class: " + q.Class + ", Method: " + q.Method);
}
}
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
The Application_UnhandledException method is not called from your method where the exception happens, so new StrackTrace() will not be meaningful, as you have discovered.
To get the stack trace for the place where the exception occurred, use e.Exception.StackTrace.
Note that the real exception may be wrapped inside another exception, possibly several layers deep (e.Exception.InnerException).
You could also use BugSense to get this information.
Disclaimer: I am one of the cofounders

DirectoryEntry.Invoke() throws error on "ChangePassword" call

DirectoryEntryObject.Invoke("ChangePassword", new object[] { oldPassword, newPassword } ); throws the following error:
"System.Reflection.TargetInvocationException:
Exception has been thrown by the
target of an invocation.
---> System.Runtime.InteropServices.COMException
(0x80020005): Type mismatch.
(Exception from HRESULT: 0x80020005
(DISP_E_TYPEMISMATCH))
--- End of inner exception stack trace ---
at System.DirectoryServices.DirectoryEntry.Invoke(String
methodName, Object[] args)
Is this something to do with any settings in the AD or I am missing anything?
There's an MSDN page titled Managing User Passwords that has some examples of how to call ChangePassword from C#. The specific sample shows the following syntax:
usr.Invoke("ChangePassword", OldSecurelyStoredPassword, NewSecurelyStoredPassword);
I suspect it's because you're calling it and passing in an object array, rather than passing two strings explicitly (the documentation you've linked to in your comment indicates that it expects to be passed two strings, one as an input parameter and one as an output parameter. Try this:
var oldPassword = "TheOldPassword";
var newPassword = "TheNewPassword";
DirectoryEntryObject.Invoke("ChangePassword", oldPassword, newPassword);

Categories

Resources