1

How I can pin secondary tile like Microsoft Edge websites, with tile ID, action, and activaton on App.xaml.cs? I want to pin opened text file on my text editor.

Update

This is my code for Pin tile:

        string tileId;

        string path = ((StorageFile)currentEditBox.Tag).Path;
        tileId = "file" + PivotMain.Items.Count;
        TileCollection colllaunch = new TileCollection();
        var mycoll = colllaunch.coll;
        mycoll.Add(tileId, path);
        // Use a display name you like
        string displayName;
        if(PivotMain.SelectedRichEditBoxItem.HeaderTextBlock.Text.Length > 10
            && currentEditBox.Tag != null)
        {
            displayName = ((StorageFile)currentEditBox.Tag).Name;
        }
        else
        {
            displayName = PivotMain.SelectedRichEditBoxItem.HeaderTextBlock.Text;
        }

        // Provide all the required info in arguments so that when user
        // clicks your tile, you can navigate them to the correct content
        string arguments = tileId;

        var imageUri = new Uri("ms-appx:///Assets/Square150x150Logo.scale-100.png");

        // During creation of secondary tile, an application may set additional arguments on the tile that will be passed in during activation.

        // Create a Secondary tile with all the required arguments.
        var secondaryTile = new SecondaryTile(tileId,
            displayName,
            arguments,
            imageUri, TileSize.Default);
        secondaryTile.VisualElements.ShowNameOnSquare150x150Logo = true;

        await secondaryTile.RequestCreateAsync();

This is code for tile collection:

    public class TileCollection
    {
        public Dictionary<string, string> coll = new Dictionary<string, string>();
    }

And this is for OnNavigatedTo event (I can't call open file event, because it is is on MainPage.xaml, and I moved it on MainPage):

        var launchArgs = e.Parameter as Windows.ApplicationModel.Activation.ILaunchActivatedEventArgs;
        if(launchArgs != null)
        {
            TileCollection colllaunch = new TileCollection();
            var mycoll = colllaunch.coll;
            if (launchArgs.TileId != "App")
            {
                StorageFile storageFile;
                string path;
                mycoll.TryGetValue(launchArgs.TileId, out path);
                storageFile = await StorageFile.GetFileFromPathAsync(path);
                await OpenFile(storageFile);
            }
        }
10 Develops
  • 39
  • 1
  • 9
  • Do you mean you want to programmatically pin a seconadry tile when your app start? Can you be specific about what you want? – Barry Wang Jul 20 '18 at 05:33
  • Yes. I want to pin secondary tile when user clicks the button, like on Microsoft Edge, where user can pin tile of websites, but I want to pin tile of text file, when user clicks on that, the app opens file from path. My app has file opening code. – 10 Develops Jul 20 '18 at 06:47

1 Answers1

0

When we set our secondary tile we will assign a tile ID like the following code:

 var secondaryTile = new SecondaryTile(“tile ID”,
  "App Name",
  "args",
  "tile",
  options,
  imageUri)
  { RoamingEnabled = true };

 await secondaryTile.RequestCreateAsync();

Then in the Onlaunched event we will be able to detect the tileID by the following code:

CollInit colllaunch = new CollInit();
var mycoll = colllaunch.coll;
if (e.PrelaunchActivated == false)
{
  if (rootFrame.Content == null)
  {
  // When the navigation stack isn't restored navigate to the first page,
  // configuring the new page by passing required information as a navigation
  // parameter
    if (e.TileId == "xxxxxx")
    {
      //Do what you want to do here
      var filepath=mycoll[xxxxxx];
      //Then navigate to a specific page
      rootFrame.Navigate(typeof(AlternatePage));

    }
    else
    {
      rootFrame.Navigate(typeof(MainPage), e.TileId);
    }
  }
  // Ensure the current window is active
  Window.Current.Activate();
}

There is a document on Technet wiki about how you can specify the content by ID.

------Update-----

For how to create a Dictionary and read it, here I just assume that you will not have a huge list, so a simple demo code:

public class CollInit
{
    public Dictionary<string, string> coll = new Dictionary<string, string>();
    public CollInit()
    {

        coll.Add("1233", "path1");
        coll.Add("1234", "path2");
        coll.Add("1235", "path3");
    }      
}

Then in onlaunched event you can read the path by create a new colloction like mycoll and then var mycoll = colllaunch.coll; after that you can read the path from mycoll[You specific TileID].

Barry Wang
  • 1,459
  • 1
  • 8
  • 12
  • I know this. But first, what set tile ID, when if I set tile ID the file path, will be incorrect? And second, The app detected tile ID with OnLaunched event, but how app can get file path to open file? – 10 Develops Jul 20 '18 at 08:20
  • @10Develops You'd better do not set the tile ID as the file path, you know the ID should be short and unique, I don't think it's a good idea for you to use file ID. Use some characters and numbers to make the ID unique is a more reasonable way. Then for the file path. What about create a dictionary yourself and store it in your app? When you get the specific title ID you can then get the value by using the ID as the key, would that be more reasonable to you? – Barry Wang Jul 23 '18 at 01:39
  • @MarmiK The answer here is just related to "Set titleid" and onlaunched event. If you are familiar with UWP platform you will know what I am saying from the words below the URL. I will edit my answer to make a full content when OP confirmed this answer can helps. – Barry Wang Jul 23 '18 at 01:49
  • @BarryWang-MSFT I am removing comment from above, as I have edited you answer to make it perfect with code. – MarmiK Jul 23 '18 at 07:16
  • @BarryWang-MSFT Thanks for code, but I know it. I think would be better create a directory for tile info, as you said. So how to do it? – 10 Develops Jul 23 '18 at 11:27
  • @10Develops To Get Get all secondary tiles Try this `var tiles = await SecondaryTile.FindAllAsync();` for more information try this [link](https://learn.microsoft.com/en-us/uwp/api/Windows.UI.StartScreen.SecondaryTile) – MarmiK Jul 23 '18 at 12:47
  • @10Develops I've added dictionary code. Actually the code is ugly in my mind. But it shows a simple way for you to think about store your simple data in class and then read it. You can also use some other data structure to save your data. – Barry Wang Jul 24 '18 at 02:27
  • @10Develops By the way, please notice that in UWP platform "path" is not a recommend thing. How did you store your text file? You may need to check the following blog first: https://blogs.msdn.microsoft.com/wsdevsol/2012/12/04/skip-the-path-stick-to-the-storagefile/ If you cannot properly get the path, then dictionary here will not work. You should make sure that you can use your code to open the file from the path you provided first. – Barry Wang Jul 24 '18 at 02:32
  • @BarryWang-MSFT, now I trying the code, but I think that would be better do a directory with StorageFile. That will be more easier and understandable. – 10 Develops Jul 24 '18 at 03:08
  • @10Develops Definitely. StorageFile would be more reasonable. – Barry Wang Jul 24 '18 at 03:10
  • @BarryWang-MSFT, I can't call OnLaunched event because Open File code is on Main Page. Because of this, I used OnNavigatedTo event on Main Page with OnLaunched event parameters. Code in top of page. – 10 Develops Jul 24 '18 at 03:50
  • @10Develops Good to see that. You can create a answer out instead of put it on your original post. This would also benefit other communities. – Barry Wang Jul 24 '18 at 05:22
  • @BarryWang-MSFT, but the code is not works. App is just launched from tile, nothing more. – 10 Develops Jul 24 '18 at 05:52
  • @10Develops OK. Didn't notice that you haven't worked it out. So please still use my code to navigate to your AlternatePage. But change the code to rootFrame.Navigate(typeof(AlternatePage),tileid); Then on that page OnNavigatedTo event, var tileid=e.Parameter as string, after that you can read the value path out. Notice that the onlaunched event is the entry and you should debug your app to see whether it goes to this event first. – Barry Wang Jul 24 '18 at 09:18
  • @BarryWang-MSFT but I don't have Alternate Page. I changed navigation parameters and if tileID = "App" condition to "", but not working. Even when activating pinned tile on already launched app, it not opening. And it can't come from OpenFile method, because it works with FileActivatedEvent. I think either tile key and value is not writing, or TryGetValue method does not work. – 10 Develops Jul 24 '18 at 10:24
  • @10Develops I'm confusing. The Alternate Page actually means you can create a single page which can handle your page navigation and then on that page you can handle the file events. And if you think any code does not work ,what about debug and check whether you can go to that line? – Barry Wang Jul 25 '18 at 09:47
  • @BarryWang-MSFT I debugged. When I launch app it just starts without opening file. – 10 Develops Jul 25 '18 at 11:32
  • @10Develops What about show us your latest code? A MVCE. – Barry Wang Jul 26 '18 at 05:32