我在尝试在 FileTypes.cs 类内的函数中引用在 xaml 页面 (ViewFilesPage.xaml) 中定义的 ApplicationBar 时遇到问题,所有这些都来自同一个项目。
ViewFilesPage.xaml 中的 ApplicationBar 代码:
<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>
FileTypes.cs中C#函数的代码:
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;
}
而不是 ViewFilesPage.ApplicationBar 我需要一个对 ApplicationBar 的工作引用,所以我可以修改按钮的数量。