我的WPF程序不会运行

时间:2014-07-03 16:04:55

标签: c# wpf

我正在创建一个简单的图片查看器应用程序,它从文件夹中获取所有图像,我可以通过单击“上一页”和“下一页”按钮查看它们之间的所有图像。

每当我运行程序时,我都会收到此错误the call stack contains only external code。我用谷歌搜索了它,我真的不知道这个错误意味着什么,而现在我只是想修复它以便我的程序可以启动。那么你们可以看看我的代码并帮助我解决问题吗?

继承XAML的窗口

<Window x:Class="PictureViewer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="700">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0">
            <Button  Name="button_Previous" Margin="0,0,10,5" HorizontalAlignment="Left"  Width="75" Height="Auto"  DockPanel.Dock="Left"  Content="Previous" Click="button_Previous_Click" />
            <Button  Name="button_Next"  Margin="0,0,10,5" HorizontalAlignment="Left"  Width="75" Height="Auto"  DockPanel.Dock="Left"  Content="Next" Click="button_Next_Click" />
            <Button  Name="button_Browse" Margin="0,0,10,5" HorizontalAlignment="Left" Width="75" Height="Auto"  DockPanel.Dock="Left"  Content="Browse"  />

        </DockPanel>
        <Rectangle Grid.Row="1" Fill="LightBlue" />
        <Image Grid.Row="1" Margin="15" Name="image_Picture" Stretch="Fill"  />
    </Grid>
</Window>

窗口背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PictureViewer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        cPicture Picture;
        public MainWindow()
        {
            InitializeComponent();
            Picture = new cPicture();
            Picture.Initialize();
            DisplayPicture();
        }

        private void button_Previous_Click(object sender, RoutedEventArgs e)
        {
            Picture.Previous();
            DisplayPicture();
        }

        private void button_Next_Click(object sender, RoutedEventArgs e)
        {
            Picture.Next();
            DisplayPicture();
        }

        private void DisplayPicture()
        {
            BitmapImage image = new BitmapImage(new Uri(Picture.PictureList[Picture.CurrentPictureNumber]));
            image_Picture.Source = image;
        }
    }
}

也是我的Picture类:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PictureViewer
{
    class cPicture
    {
        List<string> pictureList;
        int currentPictureNumber = 0;

        public List<string> PictureList 
        { 
            get { return pictureList; } 
            set { pictureList = value; } 
        }
        public int CurrentPictureNumber 
        { 
            get { return currentPictureNumber; }
        }

        public void Initialize()
        {
            Directory.GetFiles(@"C:\Users\Syeda\Desktop\Photos");
            List<string> pictureList = new List<string>(Directory.GetFiles(@"C:\Users\Syeda\Desktop\Photos"));
        }

        public void Next()
        {
            currentPictureNumber = currentPictureNumber + 1;
            if (currentPictureNumber > pictureList.Count)
            {
                currentPictureNumber = 0;
            }
        }

        public void Previous()
        {
            currentPictureNumber = currentPictureNumber - 1;
            if (currentPictureNumber < 0)
            {
                currentPictureNumber = pictureList.Count;
            }
        }

    }
}

3 个答案:

答案 0 :(得分:2)

这是您要完成的工作实现。我使用了MVVM模式。通常在构建小型WPF应用程序时,这简化了需要在场景后完成的大量工作。享受:)

PicureViewer类:

namespace Stack
{
    public class PictureViewer : INotifyPropertyChanged
    {
       private const string PictureDirectory = @"C:\Users\TMoore\Pictures";

        public int CurrentPictureNumber { get; set; }

        private string currentImagePath;
        public string CurrentImagePath
        {
            get { return currentImagePath; }
            set
            {
                currentImagePath = value;
                OnPropertyChanged();
            }
        }

        private IList<string> pictureList { get; set; }
        public IList<string> PictureList
        {
            get { return pictureList ?? (pictureList = new List<string>()); }
            set { pictureList = value; }
        }

        public PictureViewer()
        {
            // you should add a search pattern
            pictureList = Directory.EnumerateFiles(PictureDirectory)
                                   .ToList();
            CurrentImagePath = pictureList[0];
        }

        public void Next()
        {
            CurrentImagePath = CurrentPictureNumber > pictureList.Count() - 1
                ? PictureList[0]: PictureList[CurrentPictureNumber++];
        }

        public void Previous()
        {
            CurrentImagePath = CurrentPictureNumber == 0
                ? PictureList[PictureList.Count()-1] : PictureList[CurrentPictureNumber--];
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

XAML:

<Window x:Class="Stack.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0">
            <Button  Name="button_Previous" Margin="0,0,10,5" HorizontalAlignment="Left"  Width="75" Height="Auto"  DockPanel.Dock="Left"  Content="Previous" Click="button_Previous_Click" />
            <Button  Name="button_Next"  Margin="0,0,10,5" HorizontalAlignment="Left"  Width="75" Height="Auto"  DockPanel.Dock="Left"  Content="Next" Click="button_Next_Click" />
            <Button  Name="button_Browse" Margin="0,0,10,5" HorizontalAlignment="Left" Width="75" Height="Auto"  DockPanel.Dock="Left"  Content="Browse"  />

        </DockPanel>
        <Rectangle Grid.Row="1" Fill="LightBlue" />
        <Image Grid.Row="1" Margin="15" Source="{Binding CurrentImagePath, UpdateSourceTrigger=PropertyChanged}" Stretch="Fill"  />
    </Grid>
</Window>

代码背后:

public partial class MainWindow : Window
{
    PictureViewer viewer { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        DataContext = viewer ?? (viewer = new PictureViewer());
    }

    private void button_Previous_Click(object sender, RoutedEventArgs e)
    {
        viewer.Previous();
    }

    private void button_Next_Click(object sender, RoutedEventArgs e)
    {
        viewer.Next();
    }
}

答案 1 :(得分:0)

pictureList是Initialise方法中的局部变量 - 因此Global PictureList将始终为空

将Initialise更改为

public void Initialize()
    {
        pictureList = new List<string>(Directory.GetFiles(@"C:\Users\Syeda\Desktop\Photos"));
    }

(也没有必要先做冗余的GetFiles行)

答案 2 :(得分:0)

首先:

public void Initialize()
{
    Directory.GetFiles(@"C:\Users\Syeda\Desktop\Photos");
    List<string> pictureList = new List<string>(Directory.GetFiles(@"C:\Users\Syeda\Desktop\Photos"));
}

您正在创建一个局部变量pictureList,它会覆盖您的成员声明。这个方法应该是:

public void Initialize()
{        
 pictureList = new List<string>(Directory.GetFiles(@"C:\Users\Syeda\Desktop\Photos"));
}

另外,请查看调试中的picturelist并查看添加的文件。对于初学者,我建议添加“* .jpg”。这将确保只将jpg文件路径添加到pictureList。所以现在上面的方法将如下所示:

public void Initialize()
{        
 pictureList = new List<string>(Directory.GetFiles(@"C:\Users\Syeda\Desktop\Photos"), "*.jpg");
}

此外,支票代码应与“&gt; =”进行比较,如下所示:

public void Next()
    {
    currentPictureNumber = currentPictureNumber + 1;
    if (currentPictureNumber >= pictureList.Count)
        {
        currentPictureNumber = 0;
        }
    }

Previous方法也是如此:

public void Previous()
    {
    currentPictureNumber = currentPictureNumber - 1;
    if (currentPictureNumber < 0)
        {
        currentPictureNumber = pictureList.Count - 1;
        }
    }