2015-01-23 5 views
1

안녕하세요 여러분 Windows Phone 8 application에서 작업하고 있습니다. 여기에 텍스트 차단 텍스트를 배치하는 데 문제가 있습니다.wp8 public string은 프로그래밍 방식으로 만든 텍스트 블록에서 줄 바꿈하지 않습니다.

내 문자열 배열이 공개되지 않았을 때 포장이 잘 작동하는 반면 내 문자열 배열을 공용으로 만들면 포장이 작동하지 않습니다 ...!

오류를 찾을 수 없습니다.

내 코드는 여기

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 = ""입니다; 이 함수에서 주석 처리 된 것은 public 문자열이 줄 바꾸기를하지 않는 동안 줄 바꿈입니다.

문자열을 외부에 사용하려면 문자열을 공개해야합니다.

 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];          
      }  
     } 

답변

0

는 여기에 게시 코드를 보면, 문제는 결코에서 ScrollViewer로 설정 포장 속성을 사용하여 TextBlock에 추가되지 않았 것으로 보인다.

scroll.Content = questions[0]; 

에 : 나는이 줄을 업데이트 할 것

scroll.Content = text_question; 

을 한 후 클릭 이벤트 핸들러에서 text_question의 내용을 조작 할 수 있습니다.


내가 말했듯이, 당신이 필요 이상으로 복잡하고 UI 코드를 필요한 것보다 훨씬 복잡하게 만드는 것 같습니다. 솔직히 나는 코드에서 UI 컨트롤을 만들어 Children 컬렉션에 추가해야 할 때가 왔다는 것을 생각할 수 없습니다. 일반적으로 Windows Phone은 MVVM 패턴을 사용하며 UI 레이아웃은 바인딩을 통해 수행해야합니다. 이 경우

, 나는 다음과 같은 것을 할 거라고 다음 XAML에서 그런

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 
} 

을, 나는 다음과 같은 거라고 :

<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가 모든 것을 함께 또는 그 반대로 끼워 넣는 방식을 알 필요가 없습니다.