wp8公共字符串不包装在以编程方式创建的文本块中

时间:2015-01-23 07:39:54

标签: c# windows-phone-7 windows-phone-8

嗨,大家好我正在研究Windows Phone 8 application。在这里我遇到了包装文本块文本的问题。

当我的字符串数组不公开时,包装工作正常,而当我创建我的字符串数组公开然后包装不起作用...!

我无法找到错误。

我的代码在这里

int a =1;
   ScrollViewer scroll = new ScrollViewer();

   string[] questions = new string[]
   {   "Question :\n What is OOPS?   \n\n Answer: \n  Object-oriented programming (OOP) is a programming paradigm based on the concept of objects which are data structures that contain data in the form of fields often known as attributes and code in the form of procedures often known as methods. There are a few principle concepts that form the foundation of object-oriented programming: 1- Object \n 2- Class \n 3- Abstraction \n 4- Polymorphism \n 5- Inheritance \n 6- Encapsulation \n 7- Overloading & OverRiding   "
   };

   int i;

   private void showContent()
   {
       Grid ContentPanel = new Grid();
       ContentPanel.Height = 400;
       ContentPanel.Width = 440;

       ContentPanel.Margin = new Thickness(0, 20, 0, 0);

       scroll.Height = 400;
       scroll.Width = 440;
       scroll.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
       scroll.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;

       TextBlock text_question = new TextBlock();
       text_question.Height = Double.NaN;
       text_question.Width = 420;

       // text_question.Text = "Arsal Are you going.? If user is not login then there at detail page user want to add product in his wish list then there is popup comes .... Please Login  There should be align text. when user goes to detail screen then first show batch 42 % off after that shows 0% off. more share button and like button alignment also changes slowly. user can identify easily.";

       text_question.TextWrapping = TextWrapping.Wrap;
       text_question.Margin = new Thickness(10, 10, 10, 10);

       scroll.Content = questions[0];
       ContentPanel.Children.Add(scroll);
       //scroll.Content = questions[i];
       TitlePanel.Children.Add(ContentPanel);    
 }

text_question.Text =“”;在这个函数中注释的是公共字符串不包装时的包装。

我想使用字符串输出任何函数然后字符串必须公开。

        private void next_click(object sender, RoutedEventArgs e)
        {

          //  MessageBox.Show("Here is Next Question");
            a = a + 1;
            count.Text = a.ToString();
            if (a > 1)
            {
                previous.IsEnabled = true;
            }
            if (a == 5)
            {
                next.IsEnabled = false;
            }

            if (i >= 0 && i < questions.Length)
            {
                i = i + 1;
                scroll.Content = questions[i];                                       
            }    
        }

1 个答案:

答案 0 :(得分:0)

查看您在此处发布的代码,问题似乎是您永远不会将包装属性设置的TextBlock添加到ScrollViewer。我会更新这一行:

scroll.Content = questions[0];

为:

scroll.Content = text_question;

然后在click事件处理程序中操作text_question的内容。


话虽这么说,但我认为你做的远比它需要的复杂得多,并使你的UI代码比必要的更复杂。老实说,我无法想到我曾经需要在代码中创建UI控件并将它们添加到Children集合中。一般来说,Windows Phone使用MVVM模式,您的UI布局应该通过绑定完成。

在这种情况下,我会做类似以下的事情:

public class QuestionModel : INotifyPropertyChanged
{
    private string[] _questions = new string[]
    {   
        "Question :\n What is OOPS?   \n\n Answer: \n  Object-oriented programming (OOP) is a programming paradigm based on the concept of objects which are data structures that contain data in the form of fields often known as attributes and code in the form of procedures often known as methods. There are a few principle concepts that form the foundation of object-oriented programming: 1- Object \n 2- Class \n 3- Abstraction \n 4- Polymorphism \n 5- Inheritance \n 6- Encapsulation \n 7- Overloading & OverRiding   ",
        "Question 2",
        "Question 3",
        "Question 4"
    };

    private int _selectedIndex = 0;


    public QuestionModel() {
        PrevCommand = new DelegateCommand(() => { 
            if(_selectedIndex > 0) {
                _selectedIndex--;
                selectedIndexChanged();
            }
        });
        NextCommand = new DelegateCommand(() => { 
            if(_selectedIndex < _questions.Length - 1) {
                _selectedIndex++;
                selectedIndexChanged();
            }
        });
    }

    private void selectedIndexChanged() {
        NotifyPropertyChanged("CurrentQuestion");
        NotifyPropertyChanged("QuestionText");
        NotifyPropertyChanged("IsNextEnabled");
        NotifyPropertyChanged("IsPrevEnabled");
    }

    public int CurrentQuestion
    {
        get { return _selectedIndex + 1; }
    }

    public string QuestionText
    {
        get { return _questions[_selectedIndex]; }
    }

    public bool IsNextEnabled
    {
        get { return _selectedIndex < _questions.Length - 1; }
    }

    public bool IsPreviousEnabled
    {
       get { return _selectedIndex > 0; }
    }

    private ICommand _nextCommand;
    public ICommand NextCommand
    {
        get { return _nextCommand; }
        set
        {
            _nextCommand = value;
            NotifyPropertyChanged();
        }
    }

    private ICommand _prevCommand;
    public ICommand PrevCommand
    {
        get { return _prevCommand; }
        set
        {
            _prevCommand = value;
            NotifyPropertyChanged();
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class DelegateCommand : ICommand
{
    private readonly Action _action;

    public DelegateCommand(Action action)
    {
        _action = action;
    }

    public void Execute(object parameter)
    {
        _action();
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

#pragma warning disable 67
    public event EventHandler CanExecuteChanged;
#pragma warning restore 67
}

然后在XAML中,我有以下内容:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.DataContext>
        <local:QuestionModel/>
    </Page.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <TextBlock x:Name="Question" Text="{Binding QuestionText}"/>
        </ScrollViewer>
        <TextBlock x:Name="Count" Grid.Row="1" Margin="10,10,10,0" Text="{Binding CurrentQuestion, Mode=OneWay}">        
        </TextBlock>
        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Button x:Name="previous" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10" Content="Previous" IsEnabled="{Binding IsPreviousEnabled}" Command="{Binding PrevCommand}"></Button>
            <Button x:Name="next" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10" Content="Next" IsEnabled="{Binding IsNextEnabled}" Command="{Binding NextCommand}"></Button>
        </Grid>
    </Grid>
</Page>

这样,后面的代码中没有代码,您不必以编程方式创建控件或修改内容。您只需让框架的数据绑定为您完成工作,您的模型就不需要知道UI如何将所有内容组合在一起,反之亦然。