1

I am having problems trying to reference an ApplicationBar defined in a xaml page (ViewFilesPage.xaml) in a function inside the FileTypes.cs class, all from the same project.

Code for the ApplicationBar from the ViewFilesPage.xaml:

<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True">
            <shell:ApplicationBarIconButton IconUri="Graphics/ApplicationBar/NewFile.png" Click="NewFile" Text="New File" />
            <shell:ApplicationBarIconButton IconUri="Graphics/ApplicationBar/NewFolder.png" Click="NewFolder" Text="New Folder" />
            <shell:ApplicationBarIconButton IconUri="Graphics/ApplicationBar/Delete.png" Click="DeleteFile" Text="Delete File" />
            <shell:ApplicationBarIconButton IconUri="Graphics/ApplicationBar/OpenFile.png" Click="OpenFile" Text="Open File" />
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem x:Name="CutFileButton" IsEnabled="True" Click="CutFile" Text="Cut" />
                <shell:ApplicationBarMenuItem x:Name="CopyFileButton" IsEnabled="True" Click="CopyFile" Text="Copy" />
                <shell:ApplicationBarMenuItem x:Name="PasteFileButton" IsEnabled="True" Click="PasteFile" Text="Paste" />
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>

Code of the C# function in FileTypes.cs:

public static StackPanel ShowFiles(string Path)
    {

        string[] FileNames = isoStore.GetFileNames(Path + "*");
        string[] DirectoryNames = isoStore.GetDirectoryNames(Path + "*");
        int NumberOfFiles = FileNames.Length, NumberOfDirectories = DirectoryNames.Length, i;

        StackPanel FilesStackPanel = new StackPanel();
        FilesStackPanel.Orientation = System.Windows.Controls.Orientation.Vertical;
        FilesStackPanel.Margin = FilesMenuMargin;

        if (NumberOfFiles + NumberOfDirectories == 0)
        {
            TextBlock Message = new TextBlock();

            if (ViewFilesPage.ApplicationBar.Buttons.Count > 2)
            {
                ViewFilesPage.ApplicationBar.Buttons.RemoveAt(2);
                ViewFilesPage.ApplicationBar.Buttons.RemoveAt(2);
            }

            Message.Text = "Empty directory";
            FilesStackPanel.Children.Add(Message);
        }
        else
        {
            for (i = 0; i < NumberOfDirectories; i++)
            {
                if (DirectoryNames[i] != "" && DirectoryNames[i] != "RecycleBin")
                {
                    FilesStackPanel.Children.Add(WriteFileLines(Path, DirectoryNames[i], true));
                    FilesStackPanel.Children.Add(ShowFiles(Path + DirectoryNames[i] + "/"));
                }
            }

            for (i = 0; i < NumberOfFiles; i++)
            {
                FilesStackPanel.Children.Add(WriteFileLines(Path, FileNames[i], false));
            }
        }
        return FilesStackPanel;
    }

And instead of ViewFilesPage.ApplicationBar I need a working reference to the ApplicationBar, so I can modify the number of buttons.

  • 2
    Why not just pass the ApplicationBar as a function parameter? Anyway, you can use ((PhoneApplicationPage)((PhoneApplicationFrame) Application.Current.RootVisual).Content).ApplicationBar – William Melani Apr 05 '12 at 18:31
  • Thanks a lot guys, but I managed to find another solution to the proble without having to pass the application bar as a parameter (it wasn't the right thing in my case). Either way thanks for all your help! – Razvan-Catalin Olaru Apr 08 '12 at 21:38

0 Answers0