Change application default tile in runtime on Windows 10 UWP - c#

I need to change color of application tile to accent color.
On wp8 I used ShellTile class and change icon to transparent, so this solution works.
But on Windows 10 ShellTile is missing. There is another possible method using TileUpdateManager, but it's changing icon for LiveTile, not for default Tile Icon.
This code works for updating LiveTile, but not for default icon.
TileContent content = new TileContent()
{
Visual = new TileVisual()
{
TileMedium = new TileBinding()
{
Content = new TileBindingContentAdaptive()
{
BackgroundImage = new TileBackgroundImage()
{
Source = new TileImageSource("Assets/Tiles/FlipCycleTileMediumOp.png")
}
}
}
}
};
TileUpdateManager.CreateTileUpdaterForApplication().Update(new TileNotification(content.GetXml()));
And this kinda resolve the problem, but when I disable "live tile" option, icon returns to default.

Related

How to change color title bar in MAUI?

I want to change title bar color like theme color.
title bar
I get theme color but i dont know how to change the color. I try
#if WINDOWS
var uiSettings = new Windows.UI.ViewManagement.UISettings();
var color = uiSettings.GetColorValue(UIColorType.Accent);
var appView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();
var titleBar = appView.TitleBar;
titleBar.BackgroundColor = color;
#endif
I get exception System.Runtime.InteropServices.COMException: 'Element not found.
Sorry for my bad english.
According to the official document about Title bar customization in the WinUI3, you should use the WinUI3's api to do that. The code you used is for the uwp not the winui3.
So you can try the following code:
#if WINDOWS
            var uiSettings = new Windows.UI.ViewManagement.UISettings();
            var color = uiSettings.GetColorValue(UIColorType.Accent);
            Microsoft.UI.Xaml.Window window = (Microsoft.UI.Xaml.Window)App.Current.Windows.First<Window>().Handler.PlatformView;
//get the current window on the windows platform
            IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
            Microsoft.UI.WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle);
            Microsoft.UI.Windowing.AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
            Microsoft.UI.Windowing.AppWindowTitleBar titlebar = appWindow.TitleBar;
           //titlebar.ExtendsContentIntoTitleBar = true;
// in the official document, this line is needed, but when I used it, the background color didn't change
            titlebar.BackgroundColor = color;
#endif
I refered to this issue about customizing the title bar in the maui. It may provide you more ideas.

Windows Lock Screen, add Text programmatically

I want to display custom text or control on the Windows 10 Lockscreen, when I click on a button. I tried it with an UWP Application.
My goal is something like this:
And the Code I tried:
ToastContent content = new ToastContent()
{
//Duration = ToastDuration.Long,
Scenario = ToastScenario.Reminder,
Visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Attribution = new ToastGenericAttributionText()
{
Text = "Hello World"
}
}
},
Actions = new ToastActionsCustom()
{
Buttons = {
new ToastButton ("mycontent", "myargs")
}
}
};
var notification = new ToastNotification(content.GetXml());
ToastNotificationManager.CreateToastNotifier().Show(notification);
Also I saw this post and tried it of course, but it wasnt helpfull: Windows Lock Screen display text programmatically C#
Maybe you could help me to achive my goald
I thank you in advance
The screenshot you post above is smtc that used to show current playing music, for enable it you need to enable the app Background Media Playback, but it only use to show the media info, it can't use to share custom info like you mentioned scenario.
For your scenario, the better way is register your app LockScreen capability like the following.
<Applications>
<Application Id="App"
Executable="$targetnametoken$.exe"
EntryPoint="xxxxx.UWP.App">
.......
<uap:LockScreen BadgeLogo="Assets\BadgeLogo.png" Notification="badgeAndTileText"/>
</uap:VisualElements>
</Application>
</Applications>
And set the app as main toast in the lock screen Setting page -> Personalization -> lock screen -> Choose one app to show detailed status on the lock screen If you have resisted the app, it will show in the apps list.
Code Sample
private void Button_Click(object sender, RoutedEventArgs e)
{
TileContent content = new TileContent()
{
Visual = new TileVisual()
{
LockDetailedStatus1 = "Hello world",
TileWide = new TileBinding() { }
}
};
var notification = new TileNotification(content.GetXml());
TileUpdateManager.CreateTileUpdaterForApplication().Update(notification);
}

Notification bar appears all white in light mode

I'm using Template 10 and in Windows 10 Mobile when I choose the light mode, the notifications bar appears all white
and can not see the notifications, the hours, etc.
In the dark mode everything seems fine image:
How do I solve this?
I do this in my hamburger's override for UIElement CreateRootElement() after my database setup/migrations are done.
if(Template10.Utils.DeviceUtils.Current().IsPhone()){
var statusBar = Windows.UI.ViewManagement.StatusBar.GetForCurrentView();
if(statusBar != null)
{
if(Application.Current.RequestedTheme == ApplicationTheme.Light)
//background && foreground or combination, and dependent on color choices
statusBar.ForegroundColor = Windows.UI.Colors.Black;
else if(Application.Current.RequestedTheme == ApplicationTheme.Dark
statusBar.ForegroundColor = Windows.UI.Colors.White;
}
}
Template10 already has a lot of the logic built in just have to know where it is. As #Jay Zuo said you have to also include the Mobile reference as well..
As #mvermef said, to solve this problem, we can set the color used in status bar according to application's theme. We can get application's theme by using Application.RequestedTheme property and set status bar's color by using properties in Status​Bar Class. For a simple example:
public MainPage()
{
InitializeComponent();
NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar"))
{
var statusBar = StatusBar.GetForCurrentView();
if (statusBar != null)
{
if (Application.Current.RequestedTheme == ApplicationTheme.Light)
{
statusBar.ForegroundColor = Windows.UI.Colors.Black;
}
else if (Application.Current.RequestedTheme == ApplicationTheme.Dark)
{
statusBar.ForegroundColor = Windows.UI.Colors.White;
}
}
}
}
Please note to use Status​Bar Class, we need reference Windows Mobile Extensions for the UWP in the project.

Changing Theme Color in Windows 10 store App

//This is the code in App.Xaml.cs
private void DetermineAppTheme()
{
bool value = true;
if (ApplicationData.Current.LocalSettings.Values.ContainsKey("IsLightTheme"))
{
value = ((bool)ApplicationData.Current.LocalSettings.Values["IsLightTheme"]);
}
if (value == true)
{
this.RequestedTheme = (ApplicationTheme)ElementTheme.Light;
}
else
{
this.RequestedTheme = (ApplicationTheme)ElementTheme.Dark;
}
}
This is the code that I was previously using to change color in a Windows 8/8.1 Store app but unfortunately this is not working on windows 10 app.
In my settings page I am changing the state of theme on click using these lines of code
ApplicationData.Current.LocalSettings.Values["IsLightTheme"] = false;
MessageDialog messageDialog = new MessageDialog("Please restart the Application so that Theme change can take place");
await messageDialog.ShowAsync();
Why is this not changing the color from white to black, I don't understand. This works fine if implemented on a Windows 8 app.
My guess is that in your App.xaml.cs file, you're setting value = true; and then if(value) { // set light theme }
You need to set light theme if value is false.
I myself found the solution. First of all
this.RequestedTheme = (ApplicationTheme)ElementTheme.Light;
has to be changed to
this.RequestedTheme = (ApplicationTheme)ElementTheme.Default;
and in app.xaml Requested Theme has to be removed. and then this code will change the theme/

WPF: Switching window styles

I have a program that has some buttons, one of them is used to switch "Themes".
There are two themes, one is the normal Windows theme and the other is called Style2.
This is how I tried the switching
private bool UsingWindowsStyle = true;
private ResourceDictionary Style2= new ResourceDictionary() { Source = new Uri("/...;component/Resources/DefaultStyles.xaml", UriKind.RelativeOrAbsolute) };
private void SwitchButton_Click(object sender, RoutedEventArgs e)
{
if (UsingWindowsStyle)
{
Resources.MergedDictionaries.Add(Style2);
UsingWindowsStyle = false;
}
else
{
Resources.MergedDictionaries.Remove(Style2);
UsingWindowsStyle = true;
}
}
My problem is, when I use this program, and press this Button, this is what happens:
Window Opened Program operating normally with Windows theme.
SwitchButton First Click Program changes visuals to the Style2 theme. All the program's buttons operating normally.
SwitchButton Second Click Program reverts back to Windows theme, but all the buttons in the program seize to work.
Points to Consider
The program does not throw any exceptions at this point.
Debugging the code, it seems that after the second click, the program does not enter the SwitchButton_Click method.
I tried readding the Click EventHandler but with no use.
SwitchButton.Click += new RoutedEventHandler(SwitchButton_Click);
Thanks in advance for your help.
I would suggest that you are trying too hard. All you need to do is to change the Style on the Window itself. Leave the dictionaries alone. :-)
Here is an example that changes a windows style when you click from the list of available styles.
My command boils down to
//Here I am changing the style on the window
NewWindow.Style = ((StyleDetailsViewModel)x).Style;
NewWindow.Show();
with various input data
public StylingViewModel(Func<string, Style> findStyle)
{
Styles = new StyleDetailsViewModel[]
{
new StyleDetailsViewModel
{
Name = "None",
Description = "Completely remove all styling and show the raw NavigationWindow including default navigation elements",
WindowStyleNone = false,
Image = "\\Resources\\WindowStyleNone.png"
},
new StyleDetailsViewModel
{
Name = "PlainWindow",
Style = findStyle("PlainWindow"),
Description = "Hides the navigation elemetns of the NavigationWindow to make it look just like a normal window",
WindowStyleNone = false,
Image = "\\Resources\\WindowStylePlain.png"
},
new StyleDetailsViewModel
{
Name = "Windows 7",
Style = findStyle("Win7NavigationWindow"),
Description = "Uses glass effects to create a window that looks almost identical to the control panel from Windows 7.",
WindowStyleNone = false,
Image = "\\Resources\\WindowStyleWin7Nav.png"
},
and
this.DataContext = new StylingViewModel(x => (Style)this.FindResource(x));
Also beware of certain Window properties that can only be set before the window opens, such as WindowStyle="None" which you need if you are doing custom chrome.

Categories